| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| example.js | 100% | (83 / 83) | 100% | (73 / 73) | 100% | (12 / 12) | 100% | (83 / 83) | |
| lib.npmtest_plotly.js.js | 100% | (16 / 16) | 100% | (14 / 14) | 100% | (3 / 3) | 100% | (16 / 16) | |
| test.js | 100% | (54 / 54) | 100% | (39 / 39) | 100% | (13 / 13) | 100% | (54 / 54) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | 2 2 2 2 2 2 2 1 2 2 2 2 1 2 2 2 2 2 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 2 3 3 3 3 1 3 3 3 1 3 1 1 1 1 1 1 1 1 1 1 1 1 6 6 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /*
example.js
quickstart example
instruction
1. save this script as example.js
2. run the shell command:
$ npm install npmtest-plotly.js && PORT=8081 node example.js
3. play with the browser-demo on http://127.0.0.1:8081
*/
/* istanbul instrument in package npmtest_plotly_js */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
// init utility2_rollup
local = local.global.utility2_rollup || (local.modeJs === 'browser'
? local.global.utility2_npmtest_plotly_js
: global.utility2_moduleExports);
// export local
local.global.local = local;
}());
switch (local.modeJs) {
// post-init
// run browser js-env code - post-init
/* istanbul ignore next */
case 'browser':
local.testRunBrowser = function (event) {
Eif (!event || (event &&
event.currentTarget &&
event.currentTarget.className &&
event.currentTarget.className.includes &&
event.currentTarget.className.includes('onreset'))) {
// reset output
Array.from(
document.querySelectorAll('body > .resettable')
).forEach(function (element) {
switch (element.tagName) {
case 'INPUT':
case 'TEXTAREA':
element.value = '';
break;
default:
element.textContent = '';
}
});
}
switch (event && event.currentTarget && event.currentTarget.id) {
case 'testRunButton1':
// show tests
Eif (document.querySelector('#testReportDiv1').style.display === 'none') {
document.querySelector('#testReportDiv1').style.display = 'block';
document.querySelector('#testRunButton1').textContent =
'hide internal test';
local.modeTest = true;
local.testRunDefault(local);
// hide tests
} else {
document.querySelector('#testReportDiv1').style.display = 'none';
document.querySelector('#testRunButton1').textContent = 'run internal test';
}
break;
// custom-case
default:
break;
}
Iif (document.querySelector('#inputTextareaEval1') && (!event || (event &&
event.currentTarget &&
event.currentTarget.className &&
event.currentTarget.className.includes &&
event.currentTarget.className.includes('oneval')))) {
// try to eval input-code
try {
/*jslint evil: true*/
eval(document.querySelector('#inputTextareaEval1').value);
} catch (errorCaught) {
console.error(errorCaught);
}
}
};
// log stderr and stdout to #outputTextareaStdout1
['error', 'log'].forEach(function (key) {
console[key + '_original'] = console[key];
console[key] = function () {
var element;
console[key + '_original'].apply(console, arguments);
element = document.querySelector('#outputTextareaStdout1');
Iif (!element) {
return;
}
// append text to #outputTextareaStdout1
element.value += Array.from(arguments).map(function (arg) {
return typeof arg === 'string'
? arg
: JSON.stringify(arg, null, 4);
}).join(' ') + '\n';
// scroll textarea to bottom
element.scrollTop = element.scrollHeight;
};
});
// init event-handling
['change', 'click', 'keyup'].forEach(function (event) {
Array.from(document.querySelectorAll('.on' + event)).forEach(function (element) {
element.addEventListener(event, local.testRunBrowser);
});
});
// run tests
local.testRunBrowser();
break;
// run node js-env code - post-init
/* istanbul ignore next */
case 'node':
// export local
module.exports = local;
// require modules
local.fs = require('fs');
local.http = require('http');
local.url = require('url');
// init assets
local.assetsDict = local.assetsDict || {};
/* jslint-ignore-begin */
local.assetsDict['/assets.index.template.html'] = '\
<!doctype html>\n\
<html lang="en">\n\
<head>\n\
<meta charset="UTF-8">\n\
<meta name="viewport" content="width=device-width, initial-scale=1">\n\
<title>{{env.npm_package_name}} (v{{env.npm_package_version}})</title>\n\
<style>\n\
/*csslint\n\
box-sizing: false,\n\
universal-selector: false\n\
*/\n\
* {\n\
box-sizing: border-box;\n\
}\n\
body {\n\
background: #dde;\n\
font-family: Arial, Helvetica, sans-serif;\n\
margin: 2rem;\n\
}\n\
body > * {\n\
margin-bottom: 1rem;\n\
}\n\
.utility2FooterDiv {\n\
margin-top: 20px;\n\
text-align: center;\n\
}\n\
</style>\n\
<style>\n\
/*csslint\n\
*/\n\
textarea {\n\
font-family: monospace;\n\
height: 10rem;\n\
width: 100%;\n\
}\n\
textarea[readonly] {\n\
background: #ddd;\n\
}\n\
</style>\n\
</head>\n\
<body>\n\
<!-- utility2-comment\n\
<div id="ajaxProgressDiv1" style="background: #d00; height: 2px; left: 0; margin: 0; padding: 0; position: fixed; top: 0; transition: background 0.5s, width 1.5s; width: 25%;"></div>\n\
utility2-comment -->\n\
<h1>\n\
<!-- utility2-comment\n\
<a\n\
{{#if env.npm_package_homepage}}\n\
href="{{env.npm_package_homepage}}"\n\
{{/if env.npm_package_homepage}}\n\
target="_blank"\n\
>\n\
utility2-comment -->\n\
{{env.npm_package_name}} (v{{env.npm_package_version}})\n\
<!-- utility2-comment\n\
</a>\n\
utility2-comment -->\n\
</h1>\n\
<h3>{{env.npm_package_description}}</h3>\n\
<!-- utility2-comment\n\
<h4><a download href="assets.app.js">download standalone app</a></h4>\n\
<button class="onclick onreset" id="testRunButton1">run internal test</button><br>\n\
<div id="testReportDiv1" style="display: none;"></div>\n\
utility2-comment -->\n\
\n\
\n\
\n\
<label>stderr and stdout</label>\n\
<textarea class="resettable" id="outputTextareaStdout1" readonly></textarea>\n\
<!-- utility2-comment\n\
{{#if isRollup}}\n\
<script src="assets.app.js"></script>\n\
{{#unless isRollup}}\n\
utility2-comment -->\n\
<script src="assets.utility2.rollup.js"></script>\n\
<script src="jsonp.utility2._stateInit?callback=window.utility2._stateInit"></script>\n\
<script src="assets.npmtest_plotly_js.rollup.js"></script>\n\
<script src="assets.example.js"></script>\n\
<script src="assets.test.js"></script>\n\
<!-- utility2-comment\n\
{{/if isRollup}}\n\
utility2-comment -->\n\
<div class="utility2FooterDiv">\n\
[ this app was created with\n\
<a href="https://github.com/kaizhu256/node-utility2" target="_blank">utility2</a>\n\
]\n\
</div>\n\
</body>\n\
</html>\n\
';
/* jslint-ignore-end */
Iif (local.templateRender) {
local.assetsDict['/'] = local.templateRender(
local.assetsDict['/assets.index.template.html'],
{
env: local.objectSetDefault(local.env, {
npm_package_description: 'the greatest app in the world!',
npm_package_name: 'my-app',
npm_package_nameAlias: 'my_app',
npm_package_version: '0.0.1'
})
}
);
} else {
local.assetsDict['/'] = local.assetsDict['/assets.index.template.html']
.replace((/\{\{env\.(\w+?)\}\}/g), function (match0, match1) {
// jslint-hack
String(match0);
switch (match1) {
case 'npm_package_description':
return 'the greatest app in the world!';
case 'npm_package_name':
return 'my-app';
case 'npm_package_nameAlias':
return 'my_app';
case 'npm_package_version':
return '0.0.1';
}
});
}
// run the cli
Eif (local.global.utility2_rollup || module !== require.main) {
break;
}
local.assetsDict['/assets.example.js'] =
local.assetsDict['/assets.example.js'] ||
local.fs.readFileSync(__filename, 'utf8');
// bug-workaround - long $npm_package_buildCustomOrg
/* jslint-ignore-begin */
local.assetsDict['/assets.npmtest_plotly_js.rollup.js'] =
local.assetsDict['/assets.npmtest_plotly_js.rollup.js'] ||
local.fs.readFileSync(
local.npmtest_plotly_js.__dirname + '/lib.npmtest_plotly_js.js',
'utf8'
).replace((/^#!/), '//');
/* jslint-ignore-end */
local.assetsDict['/favicon.ico'] = local.assetsDict['/favicon.ico'] || '';
// if $npm_config_timeout_exit exists,
// then exit this process after $npm_config_timeout_exit ms
if (Number(process.env.npm_config_timeout_exit)) {
setTimeout(process.exit, Number(process.env.npm_config_timeout_exit));
}
// start server
if (local.global.utility2_serverHttp1) {
break;
}
process.env.PORT = process.env.PORT || '8081';
console.error('server starting on port ' + process.env.PORT);
local.http.createServer(function (request, response) {
request.urlParsed = local.url.parse(request.url);
if (local.assetsDict[request.urlParsed.pathname] !== undefined) {
response.end(local.assetsDict[request.urlParsed.pathname]);
return;
}
response.statusCode = 404;
response.end();
}).listen(process.env.PORT);
break;
}
}());
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 2 2 2 2 2 2 2 1 2 2 2 2 1 1 1 1 | /* istanbul instrument in package npmtest_plotly_js */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
// init utility2_rollup
local = local.global.utility2_rollup || local;
// init lib
local.local = local.npmtest_plotly_js = local;
// init exports
if (local.modeJs === 'browser') {
local.global.utility2_npmtest_plotly_js = local;
} else {
module.exports = local;
module.exports.__dirname = __dirname;
module.exports.module = module;
}
}());
}());
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | 2 2 2 2 2 2 2 1 2 2 1 1 1 1 2 2 2 2 1 1 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 2 1 2 2 1 2 2 1 1 1 1 1 | /* istanbul instrument in package npmtest_plotly_js */
/*jslint
bitwise: true,
browser: true,
maxerr: 8,
maxlen: 96,
node: true,
nomen: true,
regexp: true,
stupid: true
*/
(function () {
'use strict';
var local;
// run shared js-env code - pre-init
(function () {
// init local
local = {};
// init modeJs
local.modeJs = (function () {
try {
return typeof navigator.userAgent === 'string' &&
typeof document.querySelector('body') === 'object' &&
typeof XMLHttpRequest.prototype.open === 'function' &&
'browser';
} catch (errorCaughtBrowser) {
return module.exports &&
typeof process.versions.node === 'string' &&
typeof require('http').createServer === 'function' &&
'node';
}
}());
// init global
local.global = local.modeJs === 'browser'
? window
: global;
switch (local.modeJs) {
// re-init local from window.local
case 'browser':
local = local.global.utility2.objectSetDefault(
local.global.utility2_rollup || local.global.local,
local.global.utility2
);
break;
// re-init local from example.js
case 'node':
local = (local.global.utility2_rollup || require('utility2'))
.requireReadme();
break;
}
// export local
local.global.local = local;
}());
// run shared js-env code - function
(function () {
return;
}());
switch (local.modeJs) {
// run browser js-env code - function
case 'browser':
break;
// run node js-env code - function
case 'node':
break;
}
// run shared js-env code - post-init
(function () {
return;
}());
switch (local.modeJs) {
// run browser js-env code - post-init
case 'browser':
local.testCase_browser_nullCase = local.testCase_browser_nullCase || function (
options,
onError
) {
/*
* this function will test browsers's null-case handling-behavior-behavior
*/
onError(null, options);
};
// run tests
local.nop(local.modeTest &&
document.querySelector('#testRunButton1') &&
document.querySelector('#testRunButton1').click());
break;
// run node js-env code - post-init
/* istanbul ignore next */
case 'node':
local.testCase_buildApidoc_default = local.testCase_buildApidoc_default || function (
options,
onError
) {
/*
* this function will test buildApidoc's default handling-behavior-behavior
*/
options = { modulePathList: module.paths };
local.buildApidoc(options, onError);
};
local.testCase_buildApp_default = local.testCase_buildApp_default || function (
options,
onError
) {
/*
* this function will test buildApp's default handling-behavior-behavior
*/
local.testCase_buildReadme_default(options, local.onErrorThrow);
local.testCase_buildLib_default(options, local.onErrorThrow);
local.testCase_buildTest_default(options, local.onErrorThrow);
local.testCase_buildCustomOrg_default(options, local.onErrorThrow);
options = [];
local.buildApp(options, onError);
};
local.testCase_buildCustomOrg_default = local.testCase_buildCustomOrg_default ||
function (options, onError) {
/*
* this function will test buildCustomOrg's default handling-behavior
*/
options = {};
local.buildCustomOrg(options, onError);
};
local.testCase_buildLib_default = local.testCase_buildLib_default || function (
options,
onError
) {
/*
* this function will test buildLib's default handling-behavior
*/
options = {};
local.buildLib(options, onError);
};
local.testCase_buildReadme_default = local.testCase_buildReadme_default || function (
options,
onError
) {
/*
* this function will test buildReadme's default handling-behavior-behavior
*/
options = {};
local.buildReadme(options, onError);
};
local.testCase_buildTest_default = local.testCase_buildTest_default || function (
options,
onError
) {
/*
* this function will test buildTest's default handling-behavior
*/
options = {};
local.buildTest(options, onError);
};
local.testCase_webpage_default = local.testCase_webpage_default || function (
options,
onError
) {
/*
* this function will test webpage's default handling-behavior
*/
options = { modeCoverageMerge: true, url: local.serverLocalHost + '?modeTest=1' };
local.browserTest(options, onError);
};
// run test-server
local.testRunServer(local);
break;
}
}());
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| plotcss.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| ploticon.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 20 20 20 20 20 | 'use strict';
var Lib = require('../src/lib');
var rules = {
"X,X div": "font-family:'Open Sans', verdana, arial, sans-serif;margin:0;padding:0;",
"X input,X button": "font-family:'Open Sans', verdana, arial, sans-serif;",
"X input:focus,X button:focus": "outline:none;",
"X a": "text-decoration:none;",
"X a:hover": "text-decoration:none;",
"X .crisp": "shape-rendering:crispEdges;",
"X .user-select-none": "-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;-o-user-select:none;user-select:none;",
"X svg": "overflow:hidden;",
"X svg a": "fill:#447adb;",
"X svg a:hover": "fill:#3c6dc5;",
"X .main-svg": "position:absolute;top:0;left:0;pointer-events:none;",
"X .main-svg .draglayer": "pointer-events:all;",
"X .cursor-default": "cursor:default;",
"X .cursor-pointer": "cursor:pointer;",
"X .cursor-crosshair": "cursor:crosshair;",
"X .cursor-move": "cursor:move;",
"X .cursor-col-resize": "cursor:col-resize;",
"X .cursor-row-resize": "cursor:row-resize;",
"X .cursor-ns-resize": "cursor:ns-resize;",
"X .cursor-ew-resize": "cursor:ew-resize;",
"X .cursor-sw-resize": "cursor:sw-resize;",
"X .cursor-s-resize": "cursor:s-resize;",
"X .cursor-se-resize": "cursor:se-resize;",
"X .cursor-w-resize": "cursor:w-resize;",
"X .cursor-e-resize": "cursor:e-resize;",
"X .cursor-nw-resize": "cursor:nw-resize;",
"X .cursor-n-resize": "cursor:n-resize;",
"X .cursor-ne-resize": "cursor:ne-resize;",
"X .modebar": "position:absolute;top:2px;right:2px;z-index:1001;background:rgba(255,255,255,0.7);",
"X .modebar--hover": "opacity:0;-webkit-transition:opacity 0.3s ease 0s;-moz-transition:opacity 0.3s ease 0s;-ms-transition:opacity 0.3s ease 0s;-o-transition:opacity 0.3s ease 0s;transition:opacity 0.3s ease 0s;",
"X:hover .modebar--hover": "opacity:1;",
"X .modebar-group": "float:left;display:inline-block;box-sizing:border-box;margin-left:8px;position:relative;vertical-align:middle;white-space:nowrap;",
"X .modebar-group:first-child": "margin-left:0px;",
"X .modebar-btn": "position:relative;font-size:16px;padding:3px 4px;cursor:pointer;line-height:normal;box-sizing:border-box;",
"X .modebar-btn svg": "position:relative;top:2px;",
"X .modebar-btn path": "fill:rgba(0,31,95,0.3);",
"X .modebar-btn.active path,X .modebar-btn:hover path": "fill:rgba(0,22,72,0.5);",
"X .modebar-btn.modebar-btn--logo": "padding:3px 1px;",
"X .modebar-btn.modebar-btn--logo path": "fill:#447adb !important;",
"X [data-title]:before,X [data-title]:after": "position:absolute;-webkit-transform:translate3d(0, 0, 0);-moz-transform:translate3d(0, 0, 0);-ms-transform:translate3d(0, 0, 0);-o-transform:translate3d(0, 0, 0);transform:translate3d(0, 0, 0);display:none;opacity:0;z-index:1001;pointer-events:none;top:110%;right:50%;",
"X [data-title]:hover:before,X [data-title]:hover:after": "display:block;opacity:1;",
"X [data-title]:before": "content:'';position:absolute;background:transparent;border:6px solid transparent;z-index:1002;margin-top:-12px;border-bottom-color:#69738a;margin-right:-6px;",
"X [data-title]:after": "content:attr(data-title);background:#69738a;color:white;padding:8px 10px;font-size:12px;line-height:12px;white-space:nowrap;margin-right:-18px;border-radius:2px;",
"X .select-outline": "fill:none;stroke-width:1;shape-rendering:crispEdges;",
"X .select-outline-1": "stroke:white;",
"X .select-outline-2": "stroke:black;stroke-dasharray:2px 2px;",
Y: "font-family:'Open Sans';position:fixed;top:50px;right:20px;z-index:10000;font-size:10pt;max-width:180px;",
"Y p": "margin:0;",
"Y .notifier-note": "min-width:180px;max-width:250px;border:1px solid #fff;z-index:3000;margin:0;background-color:#8c97af;background-color:rgba(140,151,175,0.9);color:#fff;padding:10px;",
"Y .notifier-close": "color:#fff;opacity:0.8;float:right;padding:0 5px;background:none;border:none;font-size:20px;font-weight:bold;line-height:20px;",
"Y .notifier-close:hover": "color:#444;text-decoration:none;cursor:pointer;"
};
for(var selector in rules) {
var fullSelector = selector.replace(/^,/,' ,')
.replace(/X/g, '.js-plotly-plot .plotly')
.replace(/Y/g, '.plotly-notifier');
Lib.addStyleRule(fullSelector, rules[selector]);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 1 | 'use strict';
module.exports = {
'undo': {
'width': 857.1,
'path': 'm857 350q0-87-34-166t-91-137-137-92-166-34q-96 0-183 41t-147 114q-4 6-4 13t5 11l76 77q6 5 14 5 9-1 13-7 41-53 100-82t126-29q58 0 110 23t92 61 61 91 22 111-22 111-61 91-92 61-110 23q-55 0-105-20t-90-57l77-77q17-16 8-38-10-23-33-23h-250q-15 0-25 11t-11 25v250q0 24 22 33 22 10 39-8l72-72q60 57 137 88t159 31q87 0 166-34t137-92 91-137 34-166z',
'ascent': 850,
'descent': -150
},
'home': {
'width': 928.6,
'path': 'm786 296v-267q0-15-11-26t-25-10h-214v214h-143v-214h-214q-15 0-25 10t-11 26v267q0 1 0 2t0 2l321 264 321-264q1-1 1-4z m124 39l-34-41q-5-5-12-6h-2q-7 0-12 3l-386 322-386-322q-7-4-13-4-7 2-12 7l-35 41q-4 5-3 13t6 12l401 334q18 15 42 15t43-15l136-114v109q0 8 5 13t13 5h107q8 0 13-5t5-13v-227l122-102q5-5 6-12t-4-13z',
'ascent': 850,
'descent': -150
},
'camera-retro': {
'width': 1000,
'path': 'm518 386q0 8-5 13t-13 5q-37 0-63-27t-26-63q0-8 5-13t13-5 12 5 5 13q0 23 16 38t38 16q8 0 13 5t5 13z m125-73q0-59-42-101t-101-42-101 42-42 101 42 101 101 42 101-42 42-101z m-572-320h858v71h-858v-71z m643 320q0 89-62 152t-152 62-151-62-63-152 63-151 151-63 152 63 62 151z m-571 358h214v72h-214v-72z m-72-107h858v143h-462l-36-71h-360v-72z m929 143v-714q0-30-21-51t-50-21h-858q-29 0-50 21t-21 51v714q0 30 21 51t50 21h858q29 0 50-21t21-51z',
'ascent': 850,
'descent': -150
},
'zoombox': {
'width': 1000,
'path': 'm1000-25l-250 251c40 63 63 138 63 218 0 224-182 406-407 406-224 0-406-182-406-406s183-406 407-406c80 0 155 22 218 62l250-250 125 125z m-812 250l0 438 437 0 0-438-437 0z m62 375l313 0 0-312-313 0 0 312z',
'ascent': 850,
'descent': -150
},
'pan': {
'width': 1000,
'path': 'm1000 350l-187 188 0-125-250 0 0 250 125 0-188 187-187-187 125 0 0-250-250 0 0 125-188-188 186-187 0 125 252 0 0-250-125 0 187-188 188 188-125 0 0 250 250 0 0-126 187 188z',
'ascent': 850,
'descent': -150
},
'zoom_plus': {
'width': 1000,
'path': 'm1 787l0-875 875 0 0 875-875 0z m687-500l-187 0 0-187-125 0 0 187-188 0 0 125 188 0 0 187 125 0 0-187 187 0 0-125z',
'ascent': 850,
'descent': -150
},
'zoom_minus': {
'width': 1000,
'path': 'm0 788l0-876 875 0 0 876-875 0z m688-500l-500 0 0 125 500 0 0-125z',
'ascent': 850,
'descent': -150
},
'autoscale': {
'width': 1000,
'path': 'm250 850l-187 0-63 0 0-62 0-188 63 0 0 188 187 0 0 62z m688 0l-188 0 0-62 188 0 0-188 62 0 0 188 0 62-62 0z m-875-938l0 188-63 0 0-188 0-62 63 0 187 0 0 62-187 0z m875 188l0-188-188 0 0-62 188 0 62 0 0 62 0 188-62 0z m-125 188l-1 0-93-94-156 156 156 156 92-93 2 0 0 250-250 0 0-2 93-92-156-156-156 156 94 92 0 2-250 0 0-250 0 0 93 93 157-156-157-156-93 94 0 0 0-250 250 0 0 0-94 93 156 157 156-157-93-93 0 0 250 0 0 250z',
'ascent': 850,
'descent': -150
},
'tooltip_basic': {
'width': 1500,
'path': 'm375 725l0 0-375-375 375-374 0-1 1125 0 0 750-1125 0z',
'ascent': 850,
'descent': -150
},
'tooltip_compare': {
'width': 1125,
'path': 'm187 786l0 2-187-188 188-187 0 0 937 0 0 373-938 0z m0-499l0 1-187-188 188-188 0 0 937 0 0 376-938-1z',
'ascent': 850,
'descent': -150
},
'plotlylogo': {
'width': 1542,
'path': 'm0-10h182v-140h-182v140z m228 146h183v-286h-183v286z m225 714h182v-1000h-182v1000z m225-285h182v-715h-182v715z m225 142h183v-857h-183v857z m231-428h182v-429h-182v429z m225-291h183v-138h-183v138z',
'ascent': 850,
'descent': -150
},
'z-axis': {
'width': 1000,
'path': 'm833 5l-17 108v41l-130-65 130-66c0 0 0 38 0 39 0-1 36-14 39-25 4-15-6-22-16-30-15-12-39-16-56-20-90-22-187-23-279-23-261 0-341 34-353 59 3 60 228 110 228 110-140-8-351-35-351-116 0-120 293-142 474-142 155 0 477 22 477 142 0 50-74 79-163 96z m-374 94c-58-5-99-21-99-40 0-24 65-43 144-43 79 0 143 19 143 43 0 19-42 34-98 40v216h87l-132 135-133-135h88v-216z m167 515h-136v1c16 16 31 34 46 52l84 109v54h-230v-71h124v-1c-16-17-28-32-44-51l-89-114v-51h245v72z',
'ascent': 850,
'descent': -150
},
'3d_rotate': {
'width': 1000,
'path': 'm922 660c-5 4-9 7-14 11-359 263-580-31-580-31l-102 28 58-400c0 1 1 1 2 2 118 108 351 249 351 249s-62 27-100 42c88 83 222 183 347 122 16-8 30-17 44-27-2 1-4 2-6 4z m36-329c0 0 64 229-88 296-62 27-124 14-175-11 157-78 225-208 249-266 8-19 11-31 11-31 2 5 6 15 11 32-5-13-8-20-8-20z m-775-239c70-31 117-50 198-32-121 80-199 346-199 346l-96-15-58-12c0 0 55-226 155-287z m603 133l-317-139c0 0 4-4 19-14 7-5 24-15 24-15s-177-147-389 4c235-287 536-112 536-112l31-22 100 299-4-1z m-298-153c6-4 14-9 24-15 0 0-17 10-24 15z',
'ascent': 850,
'descent': -150
},
'camera': {
'width': 1000,
'path': 'm500 450c-83 0-150-67-150-150 0-83 67-150 150-150 83 0 150 67 150 150 0 83-67 150-150 150z m400 150h-120c-16 0-34 13-39 29l-31 93c-6 15-23 28-40 28h-340c-16 0-34-13-39-28l-31-94c-6-15-23-28-40-28h-120c-55 0-100-45-100-100v-450c0-55 45-100 100-100h800c55 0 100 45 100 100v450c0 55-45 100-100 100z m-400-550c-138 0-250 112-250 250 0 138 112 250 250 250 138 0 250-112 250-250 0-138-112-250-250-250z m365 380c-19 0-35 16-35 35 0 19 16 35 35 35 19 0 35-16 35-35 0-19-16-35-35-35z',
'ascent': 850,
'descent': -150
},
'movie': {
'width': 1000,
'path': 'm938 413l-188-125c0 37-17 71-44 94 64 38 107 107 107 187 0 121-98 219-219 219-121 0-219-98-219-219 0-61 25-117 66-156h-115c30 33 49 76 49 125 0 103-84 187-187 187s-188-84-188-187c0-57 26-107 65-141-38-22-65-62-65-109v-250c0-70 56-126 125-126h500c69 0 125 56 125 126l188-126c34 0 62 28 62 63v375c0 35-28 63-62 63z m-750 0c-69 0-125 56-125 125s56 125 125 125 125-56 125-125-56-125-125-125z m406-1c-87 0-157 70-157 157 0 86 70 156 157 156s156-70 156-156-70-157-156-157z',
'ascent': 850,
'descent': -150
},
'question': {
'width': 857.1,
'path': 'm500 82v107q0 8-5 13t-13 5h-107q-8 0-13-5t-5-13v-107q0-8 5-13t13-5h107q8 0 13 5t5 13z m143 375q0 49-31 91t-77 65-95 23q-136 0-207-119-9-14 4-24l74-55q4-4 10-4 9 0 14 7 30 38 48 51 19 14 48 14 27 0 48-15t21-33q0-21-11-34t-38-25q-35-16-65-48t-29-70v-20q0-8 5-13t13-5h107q8 0 13 5t5 13q0 10 12 27t30 28q18 10 28 16t25 19 25 27 16 34 7 45z m214-107q0-117-57-215t-156-156-215-58-216 58-155 156-58 215 58 215 155 156 216 58 215-58 156-156 57-215z',
'ascent': 850,
'descent': -150
},
'disk': {
'width': 857.1,
'path': 'm214-7h429v214h-429v-214z m500 0h72v500q0 8-6 21t-11 20l-157 156q-5 6-19 12t-22 5v-232q0-22-15-38t-38-16h-322q-22 0-37 16t-16 38v232h-72v-714h72v232q0 22 16 38t37 16h465q22 0 38-16t15-38v-232z m-214 518v178q0 8-5 13t-13 5h-107q-7 0-13-5t-5-13v-178q0-8 5-13t13-5h107q7 0 13 5t5 13z m357-18v-518q0-22-15-38t-38-16h-750q-23 0-38 16t-16 38v750q0 22 16 38t38 16h517q23 0 50-12t42-26l156-157q16-15 27-42t11-49z',
'ascent': 850,
'descent': -150
},
'lasso': {
'width': 1031,
'path': 'm1018 538c-36 207-290 336-568 286-277-48-473-256-436-463 10-57 36-108 76-151-13-66 11-137 68-183 34-28 75-41 114-42l-55-70 0 0c-2-1-3-2-4-3-10-14-8-34 5-45 14-11 34-8 45 4 1 1 2 3 2 5l0 0 113 140c16 11 31 24 45 40 4 3 6 7 8 11 48-3 100 0 151 9 278 48 473 255 436 462z m-624-379c-80 14-149 48-197 96 42 42 109 47 156 9 33-26 47-66 41-105z m-187-74c-19 16-33 37-39 60 50-32 109-55 174-68-42-25-95-24-135 8z m360 75c-34-7-69-9-102-8 8 62-16 128-68 170-73 59-175 54-244-5-9 20-16 40-20 61-28 159 121 317 333 354s407-60 434-217c28-159-121-318-333-355z',
'ascent': 850,
'descent': -150
},
'selectbox': {
'width': 1000,
'path': 'm0 850l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-285l0-143 143 0 0 143-143 0z m857 0l0-143 143 0 0 143-143 0z m-857-286l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z m285 0l0-143 143 0 0 143-143 0z m286 0l0-143 143 0 0 143-143 0z',
'ascent': 850,
'descent': -150
},
'spikeline': {
'width': 1000,
'path': 'M512 409c0-57-46-104-103-104-57 0-104 47-104 104 0 57 47 103 104 103 57 0 103-46 103-103z m-327-39l92 0 0 92-92 0z m-185 0l92 0 0 92-92 0z m370-186l92 0 0 93-92 0z m0-184l92 0 0 92-92 0z',
'ascent': 850,
'descent': -150
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| bar.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| box.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| calendars.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| candlestick.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| carpet.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| choropleth.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| contour.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| contourcarpet.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| contourgl.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| core.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| filter.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| groupby.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| heatmap.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| heatmapgl.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| histogram.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| histogram2d.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| histogram2dcontour.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| index-basic.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-cartesian.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-finance.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-geo.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-gl2d.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-gl3d.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index-mapbox.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| index.js | 20% | (1 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 20% | (1 / 5) | |
| mesh3d.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| ohlc.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| parcoords.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| pie.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| pointcloud.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scatter.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scatter3d.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scattercarpet.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scattergeo.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scattergl.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scattermapbox.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| scatterternary.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| surface.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/bar');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/box');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/components/calendars');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/candlestick');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/carpet');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/choropleth');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/contour');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/contourcarpet');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/contourgl');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 18 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/core');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/transforms/filter');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/transforms/groupby');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/heatmap');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/heatmapgl');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/histogram');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/histogram2d');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/histogram2dcontour');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./bar'),
require('./pie')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./bar'),
require('./box'),
require('./heatmap'),
require('./histogram'),
require('./histogram2d'),
require('./histogram2dcontour'),
require('./pie'),
require('./contour'),
require('./scatterternary')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./bar'),
require('./histogram'),
require('./pie'),
require('./ohlc'),
require('./candlestick')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./scattergeo'),
require('./choropleth')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./scattergl'),
require('./pointcloud'),
require('./heatmapgl'),
require('./contourgl'),
require('./parcoords')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./scatter3d'),
require('./surface'),
require('./mesh3d')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
Plotly.register([
require('./scattermapbox')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('./core');
// traces
Plotly.register([
require('./bar'),
require('./box'),
require('./heatmap'),
require('./histogram'),
require('./histogram2d'),
require('./histogram2dcontour'),
require('./pie'),
require('./contour'),
require('./scatterternary'),
require('./scatter3d'),
require('./surface'),
require('./mesh3d'),
require('./scattergeo'),
require('./choropleth'),
require('./scattergl'),
require('./pointcloud'),
require('./heatmapgl'),
require('./parcoords'),
require('./scattermapbox'),
require('./carpet'),
require('./scattercarpet'),
require('./contourcarpet'),
require('./ohlc'),
require('./candlestick')
]);
// transforms
//
// Please note that all *transform* methods are executed before
// all *calcTransform* methods - which could possibly lead to
// unexpected results when applying multiple transforms of different types
// to a given trace.
//
// For more info, see:
// https://github.com/plotly/plotly.js/pull/978#pullrequestreview-2403353
//
Plotly.register([
require('./filter'),
require('./groupby')
]);
// components
Plotly.register([
require('./calendars')
]);
module.exports = Plotly;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/mesh3d');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/ohlc');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/parcoords');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/pie');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/pointcloud');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scatter');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scatter3d');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scattercarpet');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scattergeo');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scattergl');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scattermapbox');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/scatterternary');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = require('../src/traces/surface');
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| core.js | 11.76% | (4 / 34) | 100% | (0 / 0) | 100% | (0 / 0) | 11.76% | (4 / 34) | |
| plotly.js | 100% | (6 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (6 / 6) | |
| registry.js | 28.17% | (20 / 71) | 0% | (0 / 34) | 0% | (0 / 8) | 29.85% | (20 / 67) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 20 20 20 20 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/*
* Export the plotly.js API methods.
*/
var Plotly = require('./plotly');
// package version injected by `npm run preprocess`
exports.version = '1.26.1';
// inject promise polyfill
require('es6-promise').polyfill();
// inject plot css
require('../build/plotcss');
// inject default MathJax config
require('./fonts/mathjax_config');
// plot api
exports.plot = Plotly.plot;
exports.newPlot = Plotly.newPlot;
exports.restyle = Plotly.restyle;
exports.relayout = Plotly.relayout;
exports.redraw = Plotly.redraw;
exports.update = Plotly.update;
exports.extendTraces = Plotly.extendTraces;
exports.prependTraces = Plotly.prependTraces;
exports.addTraces = Plotly.addTraces;
exports.deleteTraces = Plotly.deleteTraces;
exports.moveTraces = Plotly.moveTraces;
exports.purge = Plotly.purge;
exports.setPlotConfig = require('./plot_api/set_plot_config');
exports.register = require('./plot_api/register');
exports.toImage = require('./plot_api/to_image');
exports.downloadImage = require('./snapshot/download');
exports.validate = require('./plot_api/validate');
exports.addFrames = Plotly.addFrames;
exports.deleteFrames = Plotly.deleteFrames;
exports.animate = Plotly.animate;
// scatter is the only trace included by default
exports.register(require('./traces/scatter'));
// register all registrable components modules
exports.register([
require('./components/legend'),
require('./components/annotations'),
require('./components/shapes'),
require('./components/images'),
require('./components/updatemenus'),
require('./components/sliders'),
require('./components/rangeslider'),
require('./components/rangeselector')
]);
// plot icons
exports.Icons = require('../build/ploticon');
// unofficial 'beta' plot methods, use at your own risk
exports.Plots = Plotly.Plots;
exports.Fx = Plotly.Fx;
exports.Snapshot = require('./snapshot');
exports.PlotSchema = require('./plot_api/plot_schema');
exports.Queue = require('./lib/queue');
// export d3 used in the bundle
exports.d3 = require('d3');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/*
* Pack internal modules unto an object.
*
* This object is require'ed in as 'Plotly' in numerous src and test files.
* Require'ing 'Plotly' bypasses circular dependencies.
*
* Future development should move away from this pattern.
*
*/
// configuration
exports.defaultConfig = require('./plot_api/plot_config');
// plots
exports.Plots = require('./plots/plots');
exports.Axes = require('./plots/cartesian/axes');
exports.Fx = require('./plots/cartesian/graph_interact');
exports.ModeBar = require('./components/modebar');
// plot api
require('./plot_api/plot_api');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Loggers = require('./lib/loggers');
var noop = require('./lib/noop');
var pushUnique = require('./lib/push_unique');
var basePlotAttributes = require('./plots/attributes');
exports.modules = {};
exports.allCategories = {};
exports.allTypes = [];
exports.subplotsRegistry = {};
exports.transformsRegistry = {};
exports.componentsRegistry = {};
exports.layoutArrayContainers = [];
exports.layoutArrayRegexes = [];
/**
* register a module as the handler for a trace type
*
* @param {object} _module the module that will handle plotting this trace type
* @param {string} thisType
* @param {array of strings} categoriesIn all the categories this type is in,
* tested by calls: traceIs(trace, oneCategory)
* @param {object} meta meta information about the trace type
*/
exports.register = function(_module, thisType, categoriesIn, meta) {
if(exports.modules[thisType]) {
Loggers.log('Type ' + thisType + ' already registered');
return;
}
var categoryObj = {};
for(var i = 0; i < categoriesIn.length; i++) {
categoryObj[categoriesIn[i]] = true;
exports.allCategories[categoriesIn[i]] = true;
}
exports.modules[thisType] = {
_module: _module,
categories: categoryObj
};
if(meta && Object.keys(meta).length) {
exports.modules[thisType].meta = meta;
}
exports.allTypes.push(thisType);
};
/**
* register a subplot type
*
* @param {object} _module subplot module:
*
* @param {string or array of strings} attr
* attribute name in traces and layout
* @param {string or array of strings} idRoot
* root of id (setting the possible value for attrName)
* @param {object} attributes
* attribute(s) for traces of this subplot type
*
* In trace objects `attr` is the object key taking a valid `id` as value
* (the set of all valid ids is generated below and stored in idRegex).
*
* In the layout object, a or several valid `attr` name(s) can be keys linked
* to a nested attribute objects
* (the set of all valid attr names is generated below and stored in attrRegex).
*/
exports.registerSubplot = function(_module) {
var plotType = _module.name;
if(exports.subplotsRegistry[plotType]) {
Loggers.log('Plot type ' + plotType + ' already registered.');
return;
}
// relayout array handling will look for component module methods with this
// name and won't find them because this is a subplot module... but that
// should be fine, it will just fall back on redrawing the plot.
findArrayRegexps(_module);
// not sure what's best for the 'cartesian' type at this point
exports.subplotsRegistry[plotType] = _module;
};
exports.registerComponent = function(_module) {
var name = _module.name;
exports.componentsRegistry[name] = _module;
if(_module.layoutAttributes) {
if(_module.layoutAttributes._isLinkedToArray) {
pushUnique(exports.layoutArrayContainers, name);
}
findArrayRegexps(_module);
}
};
function findArrayRegexps(_module) {
if(_module.layoutAttributes) {
var arrayAttrRegexps = _module.layoutAttributes._arrayAttrRegexps;
if(arrayAttrRegexps) {
for(var i = 0; i < arrayAttrRegexps.length; i++) {
pushUnique(exports.layoutArrayRegexes, arrayAttrRegexps[i]);
}
}
}
}
/**
* Get registered module using trace object or trace type
*
* @param {object||string} trace
* trace object with prop 'type' or trace type as a string
* @return {object}
* module object corresponding to trace type
*/
exports.getModule = function(trace) {
if(trace.r !== undefined) {
Loggers.warn('Tried to put a polar trace ' +
'on an incompatible graph of cartesian ' +
'data. Ignoring this dataset.', trace
);
return false;
}
var _module = exports.modules[getTraceType(trace)];
if(!_module) return false;
return _module._module;
};
/**
* Determine if this trace type is in a given category
*
* @param {object||string} traceType
* a trace (object) or trace type (string)
* @param {string} category
* category in question
* @return {boolean}
*/
exports.traceIs = function(traceType, category) {
traceType = getTraceType(traceType);
// old plot.ly workspace hack, nothing to see here
if(traceType === 'various') return false;
var _module = exports.modules[traceType];
if(!_module) {
if(traceType && traceType !== 'area') {
Loggers.log('Unrecognized trace type ' + traceType + '.');
}
_module = exports.modules[basePlotAttributes.type.dflt];
}
return !!_module.categories[category];
};
/**
* Retrieve component module method. Falls back on noop if either the
* module or the method is missing, so the result can always be safely called
*
* @param {string} name
* name of component (as declared in component module)
* @param {string} method
* name of component module method
* @return {function}
*/
exports.getComponentMethod = function(name, method) {
var _module = exports.componentsRegistry[name];
if(!_module) return noop;
return _module[method] || noop;
};
function getTraceType(traceType) {
if(typeof traceType === 'object') traceType = traceType.type;
return traceType;
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| annotation_defaults.js | 11.48% | (7 / 61) | 0% | (0 / 37) | 0% | (0 / 2) | 11.86% | (7 / 59) | |
| arrow_paths.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| calc_autorange.js | 14.29% | (5 / 35) | 0% | (0 / 22) | 0% | (0 / 5) | 15.15% | (5 / 33) | |
| click.js | 14.29% | (5 / 35) | 0% | (0 / 26) | 0% | (0 / 3) | 15.63% | (5 / 32) | |
| convert_coords.js | 17.39% | (4 / 23) | 0% | (0 / 18) | 0% | (0 / 2) | 22.22% | (4 / 18) | |
| defaults.js | 60% | (3 / 5) | 100% | (0 / 0) | 0% | (0 / 1) | 60% | (3 / 5) | |
| draw.js | 7.57% | (19 / 251) | 0% | (0 / 162) | 0% | (0 / 20) | 7.88% | (19 / 241) | |
| draw_arrow_head.js | 12.31% | (8 / 65) | 0% | (0 / 50) | 0% | (0 / 3) | 14.55% | (8 / 55) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var Color = require('../color');
var Axes = require('../../plots/cartesian/axes');
var constants = require('../../plots/cartesian/constants');
var attributes = require('./attributes');
module.exports = function handleAnnotationDefaults(annIn, annOut, fullLayout, opts, itemOpts) {
opts = opts || {};
itemOpts = itemOpts || {};
function coerce(attr, dflt) {
return Lib.coerce(annIn, annOut, attributes, attr, dflt);
}
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
var clickToShow = coerce('clicktoshow');
if(!(visible || clickToShow)) return annOut;
coerce('opacity');
var bgColor = coerce('bgcolor');
var borderColor = coerce('bordercolor'),
borderOpacity = Color.opacity(borderColor);
coerce('borderpad');
var borderWidth = coerce('borderwidth');
var showArrow = coerce('showarrow');
coerce('text', showArrow ? ' ' : 'new text');
coerce('textangle');
Lib.coerceFont(coerce, 'font', fullLayout.font);
coerce('width');
coerce('align');
var h = coerce('height');
if(h) coerce('valign');
// positioning
var axLetters = ['x', 'y'],
arrowPosDflt = [-10, -30],
gdMock = {_fullLayout: fullLayout};
for(var i = 0; i < 2; i++) {
var axLetter = axLetters[i];
// xref, yref
var axRef = Axes.coerceRef(annIn, annOut, gdMock, axLetter, '', 'paper');
// x, y
Axes.coercePosition(annOut, gdMock, coerce, axRef, axLetter, 0.5);
if(showArrow) {
var arrowPosAttr = 'a' + axLetter,
// axref, ayref
aaxRef = Axes.coerceRef(annIn, annOut, gdMock, arrowPosAttr, 'pixel');
// for now the arrow can only be on the same axis or specified as pixels
// TODO: sometime it might be interesting to allow it to be on *any* axis
// but that would require updates to drawing & autorange code and maybe more
if(aaxRef !== 'pixel' && aaxRef !== axRef) {
aaxRef = annOut[arrowPosAttr] = 'pixel';
}
// ax, ay
var aDflt = (aaxRef === 'pixel') ? arrowPosDflt[i] : 0.4;
Axes.coercePosition(annOut, gdMock, coerce, aaxRef, arrowPosAttr, aDflt);
}
// xanchor, yanchor
coerce(axLetter + 'anchor');
// xshift, yshift
coerce(axLetter + 'shift');
}
// if you have one coordinate you should have both
Lib.noneOrAll(annIn, annOut, ['x', 'y']);
if(showArrow) {
coerce('arrowcolor', borderOpacity ? annOut.bordercolor : Color.defaultLine);
coerce('arrowhead');
coerce('arrowsize');
coerce('arrowwidth', ((borderOpacity && borderWidth) || 1) * 2);
coerce('standoff');
// if you have one part of arrow length you should have both
Lib.noneOrAll(annIn, annOut, ['ax', 'ay']);
}
if(clickToShow) {
var xClick = coerce('xclick');
var yClick = coerce('yclick');
// put the actual click data to bind to into private attributes
// so we don't have to do this little bit of logic on every hover event
annOut._xclick = (xClick === undefined) ? annOut.x : xClick;
annOut._yclick = (yClick === undefined) ? annOut.y : yClick;
}
var hoverText = coerce('hovertext');
if(hoverText) {
var hoverBG = coerce('hoverlabel.bgcolor',
Color.opacity(bgColor) ? Color.rgb(bgColor) : Color.defaultLine);
var hoverBorder = coerce('hoverlabel.bordercolor', Color.contrast(hoverBG));
Lib.coerceFont(coerce, 'hoverlabel.font', {
family: constants.HOVERFONT,
size: constants.HOVERFONTSIZE,
color: hoverBorder
});
}
coerce('captureevents', !!hoverText);
return annOut;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* centerx is a center of scaling tuned for maximum scalability of
* the arrowhead ie throughout mag=0.3..3 the head is joined smoothly
* to the line, but the endpoint moves.
* backoff is the distance to move the arrowhead, and the end of the
* line, in order to end at the right place
*
* TODO: option to have the pointed-to point a little in front of the
* end of the line, as people tend to want a bit of a gap there...
*/
module.exports = [
// no arrow
{
path: '',
backoff: 0
},
// wide with flat back
{
path: 'M-2.4,-3V3L0.6,0Z',
backoff: 0.6
},
// narrower with flat back
{
path: 'M-3.7,-2.5V2.5L1.3,0Z',
backoff: 1.3
},
// barbed
{
path: 'M-4.45,-3L-1.65,-0.2V0.2L-4.45,3L1.55,0Z',
backoff: 1.55
},
// wide line-drawn
{
path: 'M-2.2,-2.2L-0.2,-0.2V0.2L-2.2,2.2L-1.4,3L1.6,0L-1.4,-3Z',
backoff: 1.6
},
// narrower line-drawn
{
path: 'M-4.4,-2.1L-0.6,-0.2V0.2L-4.4,2.1L-4,3L2,0L-4,-3Z',
backoff: 2
},
// circle
{
path: 'M2,0A2,2 0 1,1 0,-2A2,2 0 0,1 2,0Z',
backoff: 0
},
// square
{
path: 'M2,2V-2H-2V2Z',
backoff: 0
}
];
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ARROWPATHS = require('./arrow_paths');
var fontAttrs = require('../../plots/font_attributes');
var cartesianConstants = require('../../plots/cartesian/constants');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
_isLinkedToArray: 'annotation',
visible: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not this annotation is visible.'
].join(' ')
},
text: {
valType: 'string',
role: 'info',
description: [
'Sets the text associated with this annotation.',
'Plotly uses a subset of HTML tags to do things like',
'newline (<br>), bold (<b></b>), italics (<i></i>),',
'hyperlinks (<a href=\'...\'></a>). Tags <em>, <sup>, <sub>',
'<span> are also supported.'
].join(' ')
},
textangle: {
valType: 'angle',
dflt: 0,
role: 'style',
description: [
'Sets the angle at which the `text` is drawn',
'with respect to the horizontal.'
].join(' ')
},
font: extendFlat({}, fontAttrs, {
description: 'Sets the annotation text font.'
}),
width: {
valType: 'number',
min: 1,
dflt: null,
role: 'style',
description: [
'Sets an explicit width for the text box. null (default) lets the',
'text set the box width. Wider text will be clipped.',
'There is no automatic wrapping; use <br> to start a new line.'
].join(' ')
},
height: {
valType: 'number',
min: 1,
dflt: null,
role: 'style',
description: [
'Sets an explicit height for the text box. null (default) lets the',
'text set the box height. Taller text will be clipped.'
].join(' ')
},
opacity: {
valType: 'number',
min: 0,
max: 1,
dflt: 1,
role: 'style',
description: 'Sets the opacity of the annotation (text + arrow).'
},
align: {
valType: 'enumerated',
values: ['left', 'center', 'right'],
dflt: 'center',
role: 'style',
description: [
'Sets the horizontal alignment of the `text` within the box.',
'Has an effect only if `text` spans more two or more lines',
'(i.e. `text` contains one or more <br> HTML tags) or if an',
'explicit width is set to override the text width.'
].join(' ')
},
valign: {
valType: 'enumerated',
values: ['top', 'middle', 'bottom'],
dflt: 'middle',
role: 'style',
description: [
'Sets the vertical alignment of the `text` within the box.',
'Has an effect only if an explicit height is set to override',
'the text height.'
].join(' ')
},
bgcolor: {
valType: 'color',
dflt: 'rgba(0,0,0,0)',
role: 'style',
description: 'Sets the background color of the annotation.'
},
bordercolor: {
valType: 'color',
dflt: 'rgba(0,0,0,0)',
role: 'style',
description: [
'Sets the color of the border enclosing the annotation `text`.'
].join(' ')
},
borderpad: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: [
'Sets the padding (in px) between the `text`',
'and the enclosing border.'
].join(' ')
},
borderwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: [
'Sets the width (in px) of the border enclosing',
'the annotation `text`.'
].join(' ')
},
// arrow
showarrow: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the annotation is drawn with an arrow.',
'If *true*, `text` is placed near the arrow\'s tail.',
'If *false*, `text` lines up with the `x` and `y` provided.'
].join(' ')
},
arrowcolor: {
valType: 'color',
role: 'style',
description: 'Sets the color of the annotation arrow.'
},
arrowhead: {
valType: 'integer',
min: 0,
max: ARROWPATHS.length,
dflt: 1,
role: 'style',
description: 'Sets the annotation arrow head style.'
},
arrowsize: {
valType: 'number',
min: 0.3,
dflt: 1,
role: 'style',
description: 'Sets the size (in px) of annotation arrow head.'
},
arrowwidth: {
valType: 'number',
min: 0.1,
role: 'style',
description: 'Sets the width (in px) of annotation arrow.'
},
standoff: {
valType: 'number',
min: 0,
dflt: 0,
role: 'style',
description: [
'Sets a distance, in pixels, to move the arrowhead away from the',
'position it is pointing at, for example to point at the edge of',
'a marker independent of zoom. Note that this shortens the arrow',
'from the `ax` / `ay` vector, in contrast to `xshift` / `yshift`',
'which moves everything by this amount.'
].join(' ')
},
ax: {
valType: 'any',
role: 'info',
description: [
'Sets the x component of the arrow tail about the arrow head.',
'If `axref` is `pixel`, a positive (negative) ',
'component corresponds to an arrow pointing',
'from right to left (left to right).',
'If `axref` is an axis, this is an absolute value on that axis,',
'like `x`, NOT a relative value.'
].join(' ')
},
ay: {
valType: 'any',
role: 'info',
description: [
'Sets the y component of the arrow tail about the arrow head.',
'If `ayref` is `pixel`, a positive (negative) ',
'component corresponds to an arrow pointing',
'from bottom to top (top to bottom).',
'If `ayref` is an axis, this is an absolute value on that axis,',
'like `y`, NOT a relative value.'
].join(' ')
},
axref: {
valType: 'enumerated',
dflt: 'pixel',
values: [
'pixel',
cartesianConstants.idRegex.x.toString()
],
role: 'info',
description: [
'Indicates in what terms the tail of the annotation (ax,ay) ',
'is specified. If `pixel`, `ax` is a relative offset in pixels ',
'from `x`. If set to an x axis id (e.g. *x* or *x2*), `ax` is ',
'specified in the same terms as that axis. This is useful ',
'for trendline annotations which should continue to indicate ',
'the correct trend when zoomed.'
].join(' ')
},
ayref: {
valType: 'enumerated',
dflt: 'pixel',
values: [
'pixel',
cartesianConstants.idRegex.y.toString()
],
role: 'info',
description: [
'Indicates in what terms the tail of the annotation (ax,ay) ',
'is specified. If `pixel`, `ay` is a relative offset in pixels ',
'from `y`. If set to a y axis id (e.g. *y* or *y2*), `ay` is ',
'specified in the same terms as that axis. This is useful ',
'for trendline annotations which should continue to indicate ',
'the correct trend when zoomed.'
].join(' ')
},
// positioning
xref: {
valType: 'enumerated',
values: [
'paper',
cartesianConstants.idRegex.x.toString()
],
role: 'info',
description: [
'Sets the annotation\'s x coordinate axis.',
'If set to an x axis id (e.g. *x* or *x2*), the `x` position',
'refers to an x coordinate',
'If set to *paper*, the `x` position refers to the distance from',
'the left side of the plotting area in normalized coordinates',
'where 0 (1) corresponds to the left (right) side.'
].join(' ')
},
x: {
valType: 'any',
role: 'info',
description: [
'Sets the annotation\'s x position.',
'If the axis `type` is *log*, then you must take the',
'log of your desired range.',
'If the axis `type` is *date*, it should be date strings,',
'like date data, though Date objects and unix milliseconds',
'will be accepted and converted to strings.',
'If the axis `type` is *category*, it should be numbers,',
'using the scale where each category is assigned a serial',
'number from zero in the order it appears.'
].join(' ')
},
xanchor: {
valType: 'enumerated',
values: ['auto', 'left', 'center', 'right'],
dflt: 'auto',
role: 'info',
description: [
'Sets the text box\'s horizontal position anchor',
'This anchor binds the `x` position to the *left*, *center*',
'or *right* of the annotation.',
'For example, if `x` is set to 1, `xref` to *paper* and',
'`xanchor` to *right* then the right-most portion of the',
'annotation lines up with the right-most edge of the',
'plotting area.',
'If *auto*, the anchor is equivalent to *center* for',
'data-referenced annotations or if there is an arrow,',
'whereas for paper-referenced with no arrow, the anchor picked',
'corresponds to the closest side.'
].join(' ')
},
xshift: {
valType: 'number',
dflt: 0,
role: 'style',
description: [
'Shifts the position of the whole annotation and arrow to the',
'right (positive) or left (negative) by this many pixels.'
].join(' ')
},
yref: {
valType: 'enumerated',
values: [
'paper',
cartesianConstants.idRegex.y.toString()
],
role: 'info',
description: [
'Sets the annotation\'s y coordinate axis.',
'If set to an y axis id (e.g. *y* or *y2*), the `y` position',
'refers to an y coordinate',
'If set to *paper*, the `y` position refers to the distance from',
'the bottom of the plotting area in normalized coordinates',
'where 0 (1) corresponds to the bottom (top).'
].join(' ')
},
y: {
valType: 'any',
role: 'info',
description: [
'Sets the annotation\'s y position.',
'If the axis `type` is *log*, then you must take the',
'log of your desired range.',
'If the axis `type` is *date*, it should be date strings,',
'like date data, though Date objects and unix milliseconds',
'will be accepted and converted to strings.',
'If the axis `type` is *category*, it should be numbers,',
'using the scale where each category is assigned a serial',
'number from zero in the order it appears.'
].join(' ')
},
yanchor: {
valType: 'enumerated',
values: ['auto', 'top', 'middle', 'bottom'],
dflt: 'auto',
role: 'info',
description: [
'Sets the text box\'s vertical position anchor',
'This anchor binds the `y` position to the *top*, *middle*',
'or *bottom* of the annotation.',
'For example, if `y` is set to 1, `yref` to *paper* and',
'`yanchor` to *top* then the top-most portion of the',
'annotation lines up with the top-most edge of the',
'plotting area.',
'If *auto*, the anchor is equivalent to *middle* for',
'data-referenced annotations or if there is an arrow,',
'whereas for paper-referenced with no arrow, the anchor picked',
'corresponds to the closest side.'
].join(' ')
},
yshift: {
valType: 'number',
dflt: 0,
role: 'style',
description: [
'Shifts the position of the whole annotation and arrow up',
'(positive) or down (negative) by this many pixels.'
].join(' ')
},
clicktoshow: {
valType: 'enumerated',
values: [false, 'onoff', 'onout'],
dflt: false,
role: 'style',
description: [
'Makes this annotation respond to clicks on the plot.',
'If you click a data point that exactly matches the `x` and `y`',
'values of this annotation, and it is hidden (visible: false),',
'it will appear. In *onoff* mode, you must click the same point',
'again to make it disappear, so if you click multiple points,',
'you can show multiple annotations. In *onout* mode, a click',
'anywhere else in the plot (on another data point or not) will',
'hide this annotation.',
'If you need to show/hide this annotation in response to different',
'`x` or `y` values, you can set `xclick` and/or `yclick`. This is',
'useful for example to label the side of a bar. To label markers',
'though, `standoff` is preferred over `xclick` and `yclick`.'
].join(' ')
},
xclick: {
valType: 'any',
role: 'info',
description: [
'Toggle this annotation when clicking a data point whose `x` value',
'is `xclick` rather than the annotation\'s `x` value.'
].join(' ')
},
yclick: {
valType: 'any',
role: 'info',
description: [
'Toggle this annotation when clicking a data point whose `y` value',
'is `yclick` rather than the annotation\'s `y` value.'
].join(' ')
},
hovertext: {
valType: 'string',
role: 'info',
description: [
'Sets text to appear when hovering over this annotation.',
'If omitted or blank, no hover label will appear.'
].join(' ')
},
hoverlabel: {
bgcolor: {
valType: 'color',
role: 'style',
description: [
'Sets the background color of the hover label.',
'By default uses the annotation\'s `bgcolor` made opaque,',
'or white if it was transparent.'
].join(' ')
},
bordercolor: {
valType: 'color',
role: 'style',
description: [
'Sets the border color of the hover label.',
'By default uses either dark grey or white, for maximum',
'contrast with `hoverlabel.bgcolor`.'
].join(' ')
},
font: extendFlat({}, fontAttrs, {
description: [
'Sets the hover label text font.',
'By default uses the global hover font and size,',
'with color from `hoverlabel.bordercolor`.'
].join(' ')
})
},
captureevents: {
valType: 'boolean',
role: 'info',
description: [
'Determines whether the annotation text box captures mouse move',
'and click events, or allows those events to pass through to data',
'points in the plot that may be behind the annotation. By default',
'`captureevents` is *false* unless `hovertext` is provided.',
'If you use the event `plotly_clickannotation` without `hovertext`',
'you must explicitly enable `captureevents`.'
].join(' ')
},
_deprecated: {
ref: {
valType: 'string',
role: 'info',
description: [
'Obsolete. Set `xref` and `yref` separately instead.'
].join(' ')
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var draw = require('./draw').draw;
module.exports = function calcAutorange(gd) {
var fullLayout = gd._fullLayout,
annotationList = Lib.filterVisible(fullLayout.annotations);
if(!annotationList.length || !gd._fullData.length) return;
var annotationAxes = {};
annotationList.forEach(function(ann) {
annotationAxes[ann.xref] = true;
annotationAxes[ann.yref] = true;
});
var autorangedAnnos = Axes.list(gd).filter(function(ax) {
return ax.autorange && annotationAxes[ax._id];
});
if(!autorangedAnnos.length) return;
return Lib.syncOrAsync([
draw,
annAutorange
], gd);
};
function annAutorange(gd) {
var fullLayout = gd._fullLayout;
// find the bounding boxes for each of these annotations'
// relative to their anchor points
// use the arrow and the text bg rectangle,
// as the whole anno may include hidden text in its bbox
Lib.filterVisible(fullLayout.annotations).forEach(function(ann) {
var xa = Axes.getFromId(gd, ann.xref),
ya = Axes.getFromId(gd, ann.yref),
headSize = 3 * ann.arrowsize * ann.arrowwidth || 0;
var headPlus, headMinus;
if(xa && xa.autorange) {
headPlus = headSize + ann.xshift;
headMinus = headSize - ann.xshift;
if(ann.axref === ann.xref) {
// expand for the arrowhead (padded by arrowhead)
Axes.expand(xa, [xa.r2c(ann.x)], {
ppadplus: headPlus,
ppadminus: headMinus
});
// again for the textbox (padded by textbox)
Axes.expand(xa, [xa.r2c(ann.ax)], {
ppadplus: ann._xpadplus,
ppadminus: ann._xpadminus
});
}
else {
Axes.expand(xa, [xa.r2c(ann.x)], {
ppadplus: Math.max(ann._xpadplus, headPlus),
ppadminus: Math.max(ann._xpadminus, headMinus)
});
}
}
if(ya && ya.autorange) {
headPlus = headSize - ann.yshift;
headMinus = headSize + ann.yshift;
if(ann.ayref === ann.yref) {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: headPlus,
ppadminus: headMinus
});
Axes.expand(ya, [ya.r2c(ann.ay)], {
ppadplus: ann._ypadplus,
ppadminus: ann._ypadminus
});
}
else {
Axes.expand(ya, [ya.r2c(ann.y)], {
ppadplus: Math.max(ann._ypadplus, headPlus),
ppadminus: Math.max(ann._ypadminus, headMinus)
});
}
}
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../../plotly');
module.exports = {
hasClickToShow: hasClickToShow,
onClick: onClick
};
/*
* hasClickToShow: does the given hoverData have ANY annotations which will
* turn ON if we click here? (used by hover events to set cursor)
*
* gd: graphDiv
* hoverData: a hoverData array, as included with the *plotly_hover* or
* *plotly_click* events in the `points` attribute
*
* returns: boolean
*/
function hasClickToShow(gd, hoverData) {
var sets = getToggleSets(gd, hoverData);
return sets.on.length > 0 || sets.explicitOff.length > 0;
}
/*
* onClick: perform the toggling (via Plotly.update) implied by clicking
* at this hoverData
*
* gd: graphDiv
* hoverData: a hoverData array, as included with the *plotly_hover* or
* *plotly_click* events in the `points` attribute
*
* returns: Promise that the update is complete
*/
function onClick(gd, hoverData) {
var toggleSets = getToggleSets(gd, hoverData),
onSet = toggleSets.on,
offSet = toggleSets.off.concat(toggleSets.explicitOff),
update = {},
i;
if(!(onSet.length || offSet.length)) return;
for(i = 0; i < onSet.length; i++) {
update['annotations[' + onSet[i] + '].visible'] = true;
}
for(i = 0; i < offSet.length; i++) {
update['annotations[' + offSet[i] + '].visible'] = false;
}
return Plotly.update(gd, {}, update);
}
/*
* getToggleSets: find the annotations which will turn on or off at this
* hoverData
*
* gd: graphDiv
* hoverData: a hoverData array, as included with the *plotly_hover* or
* *plotly_click* events in the `points` attribute
*
* returns: {
* on: Array (indices of annotations to turn on),
* off: Array (indices to turn off because you're not hovering on them),
* explicitOff: Array (indices to turn off because you *are* hovering on them)
* }
*/
function getToggleSets(gd, hoverData) {
var annotations = gd._fullLayout.annotations,
onSet = [],
offSet = [],
explicitOffSet = [],
hoverLen = (hoverData || []).length;
var i, j, anni, showMode, pointj, toggleType;
for(i = 0; i < annotations.length; i++) {
anni = annotations[i];
showMode = anni.clicktoshow;
if(showMode) {
for(j = 0; j < hoverLen; j++) {
pointj = hoverData[j];
if(pointj.xaxis._id === anni.xref &&
pointj.yaxis._id === anni.yref &&
pointj.xaxis.d2r(pointj.x) === anni._xclick &&
pointj.yaxis.d2r(pointj.y) === anni._yclick
) {
// match! toggle this annotation
// regardless of its clicktoshow mode
// but if it's onout mode, off is implicit
if(anni.visible) {
if(showMode === 'onout') toggleType = offSet;
else toggleType = explicitOffSet;
}
else {
toggleType = onSet;
}
toggleType.push(i);
break;
}
}
if(j === hoverLen) {
// no match - only turn this annotation OFF, and only if
// showmode is 'onout'
if(anni.visible && showMode === 'onout') offSet.push(i);
}
}
}
return {on: onSet, off: offSet, explicitOff: explicitOffSet};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var toLogRange = require('../../lib/to_log_range');
/*
* convertCoords: when converting an axis between log and linear
* you need to alter any annotations on that axis to keep them
* pointing at the same data point.
* In v2.0 this will become obsolete
*
* gd: the plot div
* ax: the axis being changed
* newType: the type it's getting
* doExtra: function(attr, val) from inside relayout that sets the attribute.
* Use this to make the changes as it's aware if any other changes in the
* same relayout call should override this conversion.
*/
module.exports = function convertCoords(gd, ax, newType, doExtra) {
ax = ax || {};
var toLog = (newType === 'log') && (ax.type === 'linear'),
fromLog = (newType === 'linear') && (ax.type === 'log');
if(!(toLog || fromLog)) return;
var annotations = gd._fullLayout.annotations,
axLetter = ax._id.charAt(0),
ann,
attrPrefix;
function convert(attr) {
var currentVal = ann[attr],
newVal = null;
if(toLog) newVal = toLogRange(currentVal, ax.range);
else newVal = Math.pow(10, currentVal);
// if conversion failed, delete the value so it gets a default value
if(!isNumeric(newVal)) newVal = null;
doExtra(attrPrefix + attr, newVal);
}
for(var i = 0; i < annotations.length; i++) {
ann = annotations[i];
attrPrefix = 'annotations[' + i + '].';
if(ann[axLetter + 'ref'] === ax._id) convert(axLetter);
if(ann['a' + axLetter + 'ref'] === ax._id) convert('a' + axLetter);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var handleArrayContainerDefaults = require('../../plots/array_container_defaults');
var handleAnnotationDefaults = require('./annotation_defaults');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
var opts = {
name: 'annotations',
handleItemDefaults: handleAnnotationDefaults
};
handleArrayContainerDefaults(layoutIn, layoutOut, opts);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Plotly = require('../../plotly');
var Plots = require('../../plots/plots');
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var Fx = require('../../plots/cartesian/graph_interact');
var Color = require('../color');
var Drawing = require('../drawing');
var svgTextUtils = require('../../lib/svg_text_utils');
var setCursor = require('../../lib/setcursor');
var dragElement = require('../dragelement');
var drawArrowHead = require('./draw_arrow_head');
// Annotations are stored in gd.layout.annotations, an array of objects
// index can point to one item in this array,
// or non-numeric to simply add a new one
// or -1 to modify all existing
// opt can be the full options object, or one key (to be set to value)
// or undefined to simply redraw
// if opt is blank, val can be 'add' or a full options object to add a new
// annotation at that point in the array, or 'remove' to delete this one
module.exports = {
draw: draw,
drawOne: drawOne
};
/*
* draw: draw all annotations without any new modifications
*/
function draw(gd) {
var fullLayout = gd._fullLayout;
fullLayout._infolayer.selectAll('.annotation').remove();
for(var i = 0; i < fullLayout.annotations.length; i++) {
if(fullLayout.annotations[i].visible) {
drawOne(gd, i);
}
}
return Plots.previousPromises(gd);
}
/*
* drawOne: draw a single annotation, potentially with modifications
*
* index (int): the annotation to draw
*/
function drawOne(gd, index) {
var layout = gd.layout,
fullLayout = gd._fullLayout,
gs = gd._fullLayout._size;
// remove the existing annotation if there is one
fullLayout._infolayer.selectAll('.annotation[data-index="' + index + '"]').remove();
// remember a few things about what was already there,
var optionsIn = (layout.annotations || [])[index],
options = fullLayout.annotations[index];
var annClipID = 'clip' + fullLayout._uid + '_ann' + index;
// this annotation is gone - quit now after deleting it
// TODO: use d3 idioms instead of deleting and redrawing every time
if(!optionsIn || options.visible === false) {
d3.selectAll('#' + annClipID).remove();
return;
}
var xa = Axes.getFromId(gd, options.xref),
ya = Axes.getFromId(gd, options.yref),
// calculated pixel positions
// x & y each will get text, head, and tail as appropriate
annPosPx = {x: {}, y: {}},
textangle = +options.textangle || 0;
// create the components
// made a single group to contain all, so opacity can work right
// with border/arrow together this could handle a whole bunch of
// cleanup at this point, but works for now
var annGroup = fullLayout._infolayer.append('g')
.classed('annotation', true)
.attr('data-index', String(index))
.style('opacity', options.opacity);
// another group for text+background so that they can rotate together
var annTextGroup = annGroup.append('g')
.classed('annotation-text-g', true)
.attr('data-index', String(index));
var annTextGroupInner = annTextGroup.append('g')
.style('pointer-events', options.captureevents ? 'all' : null)
.call(setCursor, 'default')
.on('click', function() {
gd._dragging = false;
gd.emit('plotly_clickannotation', {
index: index,
annotation: optionsIn,
fullAnnotation: options
});
});
if(options.hovertext) {
annTextGroupInner
.on('mouseover', function() {
var hoverOptions = options.hoverlabel;
var hoverFont = hoverOptions.font;
var bBox = this.getBoundingClientRect();
var bBoxRef = gd.getBoundingClientRect();
Fx.loneHover({
x0: bBox.left - bBoxRef.left,
x1: bBox.right - bBoxRef.left,
y: (bBox.top + bBox.bottom) / 2 - bBoxRef.top,
text: options.hovertext,
color: hoverOptions.bgcolor,
borderColor: hoverOptions.bordercolor,
fontFamily: hoverFont.family,
fontSize: hoverFont.size,
fontColor: hoverFont.color
}, {
container: fullLayout._hoverlayer.node(),
outerContainer: fullLayout._paper.node()
});
})
.on('mouseout', function() {
Fx.loneUnhover(fullLayout._hoverlayer.node());
});
}
var borderwidth = options.borderwidth,
borderpad = options.borderpad,
borderfull = borderwidth + borderpad;
var annTextBG = annTextGroupInner.append('rect')
.attr('class', 'bg')
.style('stroke-width', borderwidth + 'px')
.call(Color.stroke, options.bordercolor)
.call(Color.fill, options.bgcolor);
var isSizeConstrained = options.width || options.height;
var annTextClip = fullLayout._defs.select('.clips')
.selectAll('#' + annClipID)
.data(isSizeConstrained ? [0] : []);
annTextClip.enter().append('clipPath')
.classed('annclip', true)
.attr('id', annClipID)
.append('rect');
annTextClip.exit().remove();
var font = options.font;
var annText = annTextGroupInner.append('text')
.classed('annotation', true)
.attr('data-unformatted', options.text)
.text(options.text);
function textLayout(s) {
s.call(Drawing.font, font)
.attr({
'text-anchor': {
left: 'start',
right: 'end'
}[options.align] || 'middle'
});
svgTextUtils.convertToTspans(s, drawGraphicalElements);
return s;
}
function drawGraphicalElements() {
// make sure lines are aligned the way they will be
// at the end, even if their position changes
annText.selectAll('tspan.line').attr({y: 0, x: 0});
var mathjaxGroup = annTextGroupInner.select('.annotation-math-group');
var hasMathjax = !mathjaxGroup.empty();
var anntextBB = Drawing.bBox(
(hasMathjax ? mathjaxGroup : annText).node());
var textWidth = anntextBB.width;
var textHeight = anntextBB.height;
var annWidth = options.width || textWidth;
var annHeight = options.height || textHeight;
var outerWidth = Math.round(annWidth + 2 * borderfull);
var outerHeight = Math.round(annHeight + 2 * borderfull);
// save size in the annotation object for use by autoscale
options._w = annWidth;
options._h = annHeight;
function shiftFraction(v, anchor) {
if(anchor === 'auto') {
if(v < 1 / 3) anchor = 'left';
else if(v > 2 / 3) anchor = 'right';
else anchor = 'center';
}
return {
center: 0,
middle: 0,
left: 0.5,
bottom: -0.5,
right: -0.5,
top: 0.5
}[anchor];
}
var annotationIsOffscreen = false;
['x', 'y'].forEach(function(axLetter) {
var axRef = options[axLetter + 'ref'] || axLetter,
tailRef = options['a' + axLetter + 'ref'],
ax = Axes.getFromId(gd, axRef),
dimAngle = (textangle + (axLetter === 'x' ? 0 : -90)) * Math.PI / 180,
// note that these two can be either positive or negative
annSizeFromWidth = outerWidth * Math.cos(dimAngle),
annSizeFromHeight = outerHeight * Math.sin(dimAngle),
// but this one is the positive total size
annSize = Math.abs(annSizeFromWidth) + Math.abs(annSizeFromHeight),
anchor = options[axLetter + 'anchor'],
overallShift = options[axLetter + 'shift'] * (axLetter === 'x' ? 1 : -1),
posPx = annPosPx[axLetter],
basePx,
textPadShift,
alignPosition,
autoAlignFraction,
textShift;
/*
* calculate the *primary* pixel position
* which is the arrowhead if there is one,
* otherwise the text anchor point
*/
if(ax) {
/*
* hide the annotation if it's pointing outside the visible plot
* as long as the axis isn't autoranged - then we need to draw it
* anyway to get its bounding box. When we're dragging, an axis can
* still look autoranged even though it won't be when the drag finishes.
*/
var posFraction = ax.r2fraction(options[axLetter]);
if((gd._dragging || !ax.autorange) && (posFraction < 0 || posFraction > 1)) {
if(tailRef === axRef) {
posFraction = ax.r2fraction(options['a' + axLetter]);
if(posFraction < 0 || posFraction > 1) {
annotationIsOffscreen = true;
}
}
else {
annotationIsOffscreen = true;
}
if(annotationIsOffscreen) return;
}
basePx = ax._offset + ax.r2p(options[axLetter]);
autoAlignFraction = 0.5;
}
else {
if(axLetter === 'x') {
alignPosition = options[axLetter];
basePx = gs.l + gs.w * alignPosition;
}
else {
alignPosition = 1 - options[axLetter];
basePx = gs.t + gs.h * alignPosition;
}
autoAlignFraction = options.showarrow ? 0.5 : alignPosition;
}
// now translate this into pixel positions of head, tail, and text
// as well as paddings for autorange
if(options.showarrow) {
posPx.head = basePx;
var arrowLength = options['a' + axLetter];
// with an arrow, the text rotates around the anchor point
textShift = annSizeFromWidth * shiftFraction(0.5, options.xanchor) -
annSizeFromHeight * shiftFraction(0.5, options.yanchor);
if(tailRef === axRef) {
posPx.tail = ax._offset + ax.r2p(arrowLength);
// tail is data-referenced: autorange pads the text in px from the tail
textPadShift = textShift;
}
else {
posPx.tail = basePx + arrowLength;
// tail is specified in px from head, so autorange also pads vs head
textPadShift = textShift + arrowLength;
}
posPx.text = posPx.tail + textShift;
// constrain pixel/paper referenced so the draggers are at least
// partially visible
var maxPx = fullLayout[(axLetter === 'x') ? 'width' : 'height'];
if(axRef === 'paper') {
posPx.head = Lib.constrain(posPx.head, 1, maxPx - 1);
}
if(tailRef === 'pixel') {
var shiftPlus = -Math.max(posPx.tail - 3, posPx.text),
shiftMinus = Math.min(posPx.tail + 3, posPx.text) - maxPx;
if(shiftPlus > 0) {
posPx.tail += shiftPlus;
posPx.text += shiftPlus;
}
else if(shiftMinus > 0) {
posPx.tail -= shiftMinus;
posPx.text -= shiftMinus;
}
}
posPx.tail += overallShift;
posPx.head += overallShift;
}
else {
// with no arrow, the text rotates and *then* we put the anchor
// relative to the new bounding box
textShift = annSize * shiftFraction(autoAlignFraction, anchor);
textPadShift = textShift;
posPx.text = basePx + textShift;
}
posPx.text += overallShift;
textShift += overallShift;
textPadShift += overallShift;
// padplus/minus are used by autorange
options['_' + axLetter + 'padplus'] = (annSize / 2) + textPadShift;
options['_' + axLetter + 'padminus'] = (annSize / 2) - textPadShift;
// size/shift are used during dragging
options['_' + axLetter + 'size'] = annSize;
options['_' + axLetter + 'shift'] = textShift;
});
if(annotationIsOffscreen) {
annTextGroupInner.remove();
return;
}
var xShift = 0;
var yShift = 0;
if(options.align !== 'left') {
xShift = (annWidth - textWidth) * (options.align === 'center' ? 0.5 : 1);
}
if(options.valign !== 'top') {
yShift = (annHeight - textHeight) * (options.valign === 'middle' ? 0.5 : 1);
}
if(hasMathjax) {
mathjaxGroup.select('svg').attr({
x: borderfull + xShift - 1,
y: borderfull + yShift
})
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null);
}
else {
var texty = borderfull + yShift - anntextBB.top,
textx = borderfull + xShift - anntextBB.left;
annText.attr({
x: textx,
y: texty
})
.call(Drawing.setClipUrl, isSizeConstrained ? annClipID : null);
annText.selectAll('tspan.line').attr({y: texty, x: textx});
}
annTextClip.select('rect').call(Drawing.setRect, borderfull, borderfull,
annWidth, annHeight);
annTextBG.call(Drawing.setRect, borderwidth / 2, borderwidth / 2,
outerWidth - borderwidth, outerHeight - borderwidth);
annTextGroupInner.call(Drawing.setTranslate,
Math.round(annPosPx.x.text - outerWidth / 2),
Math.round(annPosPx.y.text - outerHeight / 2));
/*
* rotate text and background
* we already calculated the text center position *as rotated*
* because we needed that for autoranging anyway, so now whether
* we have an arrow or not, we rotate about the text center.
*/
annTextGroup.attr({transform: 'rotate(' + textangle + ',' +
annPosPx.x.text + ',' + annPosPx.y.text + ')'});
var annbase = 'annotations[' + index + ']';
/*
* add the arrow
* uses options[arrowwidth,arrowcolor,arrowhead] for styling
* dx and dy are normally zero, but when you are dragging the textbox
* while the head stays put, dx and dy are the pixel offsets
*/
var drawArrow = function(dx, dy) {
d3.select(gd)
.selectAll('.annotation-arrow-g[data-index="' + index + '"]')
.remove();
var headX = annPosPx.x.head,
headY = annPosPx.y.head,
tailX = annPosPx.x.tail + dx,
tailY = annPosPx.y.tail + dy,
textX = annPosPx.x.text + dx,
textY = annPosPx.y.text + dy,
// find the edge of the text box, where we'll start the arrow:
// create transform matrix to rotate the text box corners
transform = Lib.rotationXYMatrix(textangle, textX, textY),
applyTransform = Lib.apply2DTransform(transform),
applyTransform2 = Lib.apply2DTransform2(transform),
// calculate and transform bounding box
width = +annTextBG.attr('width'),
height = +annTextBG.attr('height'),
xLeft = textX - 0.5 * width,
xRight = xLeft + width,
yTop = textY - 0.5 * height,
yBottom = yTop + height,
edges = [
[xLeft, yTop, xLeft, yBottom],
[xLeft, yBottom, xRight, yBottom],
[xRight, yBottom, xRight, yTop],
[xRight, yTop, xLeft, yTop]
].map(applyTransform2);
// Remove the line if it ends inside the box. Use ray
// casting for rotated boxes: see which edges intersect a
// line from the arrowhead to far away and reduce with xor
// to get the parity of the number of intersections.
if(edges.reduce(function(a, x) {
return a ^
!!lineIntersect(headX, headY, headX + 1e6, headY + 1e6,
x[0], x[1], x[2], x[3]);
}, false)) {
// no line or arrow - so quit drawArrow now
return;
}
edges.forEach(function(x) {
var p = lineIntersect(tailX, tailY, headX, headY,
x[0], x[1], x[2], x[3]);
if(p) {
tailX = p.x;
tailY = p.y;
}
});
var strokewidth = options.arrowwidth,
arrowColor = options.arrowcolor;
var arrowGroup = annGroup.append('g')
.style({opacity: Color.opacity(arrowColor)})
.classed('annotation-arrow-g', true)
.attr('data-index', String(index));
var arrow = arrowGroup.append('path')
.attr('d', 'M' + tailX + ',' + tailY + 'L' + headX + ',' + headY)
.style('stroke-width', strokewidth + 'px')
.call(Color.stroke, Color.rgb(arrowColor));
drawArrowHead(arrow, options.arrowhead, 'end', options.arrowsize, options.standoff);
// the arrow dragger is a small square right at the head, then a line to the tail,
// all expanded by a stroke width of 6px plus the arrow line width
if(gd._context.editable && arrow.node().parentNode) {
var arrowDragHeadX = headX;
var arrowDragHeadY = headY;
if(options.standoff) {
var arrowLength = Math.sqrt(Math.pow(headX - tailX, 2) + Math.pow(headY - tailY, 2));
arrowDragHeadX += options.standoff * (tailX - headX) / arrowLength;
arrowDragHeadY += options.standoff * (tailY - headY) / arrowLength;
}
var arrowDrag = arrowGroup.append('path')
.classed('annotation', true)
.classed('anndrag', true)
.attr({
'data-index': String(index),
d: 'M3,3H-3V-3H3ZM0,0L' + (tailX - arrowDragHeadX) + ',' + (tailY - arrowDragHeadY),
transform: 'translate(' + arrowDragHeadX + ',' + arrowDragHeadY + ')'
})
.style('stroke-width', (strokewidth + 6) + 'px')
.call(Color.stroke, 'rgba(0,0,0,0)')
.call(Color.fill, 'rgba(0,0,0,0)');
var update,
annx0,
anny0;
// dragger for the arrow & head: translates the whole thing
// (head/tail/text) all together
dragElement.init({
element: arrowDrag.node(),
prepFn: function() {
var pos = Drawing.getTranslate(annTextGroupInner);
annx0 = pos.x;
anny0 = pos.y;
update = {};
if(xa && xa.autorange) {
update[xa._name + '.autorange'] = true;
}
if(ya && ya.autorange) {
update[ya._name + '.autorange'] = true;
}
},
moveFn: function(dx, dy) {
var annxy0 = applyTransform(annx0, anny0),
xcenter = annxy0[0] + dx,
ycenter = annxy0[1] + dy;
annTextGroupInner.call(Drawing.setTranslate, xcenter, ycenter);
update[annbase + '.x'] = xa ?
xa.p2r(xa.r2p(options.x) + dx) :
(options.x + (dx / gs.w));
update[annbase + '.y'] = ya ?
ya.p2r(ya.r2p(options.y) + dy) :
(options.y - (dy / gs.h));
if(options.axref === options.xref) {
update[annbase + '.ax'] = xa.p2r(xa.r2p(options.ax) + dx);
}
if(options.ayref === options.yref) {
update[annbase + '.ay'] = ya.p2r(ya.r2p(options.ay) + dy);
}
arrowGroup.attr('transform', 'translate(' + dx + ',' + dy + ')');
annTextGroup.attr({
transform: 'rotate(' + textangle + ',' +
xcenter + ',' + ycenter + ')'
});
},
doneFn: function(dragged) {
if(dragged) {
Plotly.relayout(gd, update);
var notesBox = document.querySelector('.js-notes-box-panel');
if(notesBox) notesBox.redraw(notesBox.selectedObj);
}
}
});
}
};
if(options.showarrow) drawArrow(0, 0);
// user dragging the annotation (text, not arrow)
if(gd._context.editable) {
var update,
baseTextTransform;
// dragger for the textbox: if there's an arrow, just drag the
// textbox and tail, leave the head untouched
dragElement.init({
element: annTextGroupInner.node(),
prepFn: function() {
baseTextTransform = annTextGroup.attr('transform');
update = {};
},
moveFn: function(dx, dy) {
var csr = 'pointer';
if(options.showarrow) {
if(options.axref === options.xref) {
update[annbase + '.ax'] = xa.p2r(xa.r2p(options.ax) + dx);
} else {
update[annbase + '.ax'] = options.ax + dx;
}
if(options.ayref === options.yref) {
update[annbase + '.ay'] = ya.p2r(ya.r2p(options.ay) + dy);
} else {
update[annbase + '.ay'] = options.ay + dy;
}
drawArrow(dx, dy);
}
else {
if(xa) update[annbase + '.x'] = options.x + dx / xa._m;
else {
var widthFraction = options._xsize / gs.w,
xLeft = options.x + (options._xshift - options.xshift) / gs.w -
widthFraction / 2;
update[annbase + '.x'] = dragElement.align(xLeft + dx / gs.w,
widthFraction, 0, 1, options.xanchor);
}
if(ya) update[annbase + '.y'] = options.y + dy / ya._m;
else {
var heightFraction = options._ysize / gs.h,
yBottom = options.y - (options._yshift + options.yshift) / gs.h -
heightFraction / 2;
update[annbase + '.y'] = dragElement.align(yBottom - dy / gs.h,
heightFraction, 0, 1, options.yanchor);
}
if(!xa || !ya) {
csr = dragElement.getCursor(
xa ? 0.5 : update[annbase + '.x'],
ya ? 0.5 : update[annbase + '.y'],
options.xanchor, options.yanchor
);
}
}
annTextGroup.attr({
transform: 'translate(' + dx + ',' + dy + ')' + baseTextTransform
});
setCursor(annTextGroupInner, csr);
},
doneFn: function(dragged) {
setCursor(annTextGroupInner);
if(dragged) {
Plotly.relayout(gd, update);
var notesBox = document.querySelector('.js-notes-box-panel');
if(notesBox) notesBox.redraw(notesBox.selectedObj);
}
}
});
}
}
if(gd._context.editable) {
annText.call(svgTextUtils.makeEditable, annTextGroupInner)
.call(textLayout)
.on('edit', function(_text) {
options.text = _text;
this.attr({'data-unformatted': options.text});
this.call(textLayout);
var update = {};
update['annotations[' + index + '].text'] = options.text;
if(xa && xa.autorange) {
update[xa._name + '.autorange'] = true;
}
if(ya && ya.autorange) {
update[ya._name + '.autorange'] = true;
}
Plotly.relayout(gd, update);
});
}
else annText.call(textLayout);
}
// look for intersection of two line segments
// (1->2 and 3->4) - returns array [x,y] if they do, null if not
function lineIntersect(x1, y1, x2, y2, x3, y3, x4, y4) {
var a = x2 - x1,
b = x3 - x1,
c = x4 - x3,
d = y2 - y1,
e = y3 - y1,
f = y4 - y3,
det = a * f - c * d;
// parallel lines? intersection is undefined
// ignore the case where they are colinear
if(det === 0) return null;
var t = (b * f - c * e) / det,
u = (b * d - a * e) / det;
// segments do not intersect?
if(u < 0 || u > 1 || t < 0 || t > 1) return null;
return {x: x1 + a * t, y: y1 + d * t};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Color = require('../color');
var Drawing = require('../drawing');
var ARROWPATHS = require('./arrow_paths');
// add arrowhead(s) to a path or line d3 element el3
// style: 1-6, first 5 are pointers, 6 is circle, 7 is square, 8 is none
// ends is 'start', 'end' (default), 'start+end'
// mag is magnification vs. default (default 1)
module.exports = function drawArrowHead(el3, style, ends, mag, standoff) {
if(!isNumeric(mag)) mag = 1;
var el = el3.node(),
headStyle = ARROWPATHS[style||0];
if(typeof ends !== 'string' || !ends) ends = 'end';
var scale = (Drawing.getPx(el3, 'stroke-width') || 1) * mag,
stroke = el3.style('stroke') || Color.defaultLine,
opacity = el3.style('stroke-opacity') || 1,
doStart = ends.indexOf('start') >= 0,
doEnd = ends.indexOf('end') >= 0,
backOff = headStyle.backoff * scale + standoff,
start,
end,
startRot,
endRot;
if(el.nodeName === 'line') {
start = {x: +el3.attr('x1'), y: +el3.attr('y1')};
end = {x: +el3.attr('x2'), y: +el3.attr('y2')};
var dx = start.x - end.x,
dy = start.y - end.y;
startRot = Math.atan2(dy, dx);
endRot = startRot + Math.PI;
if(backOff) {
if(backOff * backOff > dx * dx + dy * dy) {
hideLine();
return;
}
var backOffX = backOff * Math.cos(startRot),
backOffY = backOff * Math.sin(startRot);
if(doStart) {
start.x -= backOffX;
start.y -= backOffY;
el3.attr({x1: start.x, y1: start.y});
}
if(doEnd) {
end.x += backOffX;
end.y += backOffY;
el3.attr({x2: end.x, y2: end.y});
}
}
}
else if(el.nodeName === 'path') {
var pathlen = el.getTotalLength(),
// using dash to hide the backOff region of the path.
// if we ever allow dash for the arrow we'll have to
// do better than this hack... maybe just manually
// combine the two
dashArray = '';
if(pathlen < backOff) {
hideLine();
return;
}
if(doStart) {
var start0 = el.getPointAtLength(0),
dstart = el.getPointAtLength(0.1);
startRot = Math.atan2(start0.y - dstart.y, start0.x - dstart.x);
start = el.getPointAtLength(Math.min(backOff, pathlen));
if(backOff) dashArray = '0px,' + backOff + 'px,';
}
if(doEnd) {
var end0 = el.getPointAtLength(pathlen),
dend = el.getPointAtLength(pathlen - 0.1);
endRot = Math.atan2(end0.y - dend.y, end0.x - dend.x);
end = el.getPointAtLength(Math.max(0, pathlen - backOff));
if(backOff) {
var shortening = dashArray ? 2 * backOff : backOff;
dashArray += (pathlen - shortening) + 'px,' + pathlen + 'px';
}
}
else if(dashArray) dashArray += pathlen + 'px';
if(dashArray) el3.style('stroke-dasharray', dashArray);
}
function hideLine() { el3.style('stroke-dasharray', '0px,100px'); }
function drawhead(p, rot) {
if(!headStyle.path) return;
if(style > 5) rot = 0; // don't rotate square or circle
d3.select(el.parentElement).append('path')
.attr({
'class': el3.attr('class'),
d: headStyle.path,
transform:
'translate(' + p.x + ',' + p.y + ')' +
'rotate(' + (rot * 180 / Math.PI) + ')' +
'scale(' + scale + ')'
})
.style({
fill: stroke,
opacity: opacity,
'stroke-width': 0
});
}
if(doStart) drawhead(start, startRot);
if(doEnd) drawhead(end, endRot);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| calendars.js | 100% | (16 / 16) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (16 / 16) | |
| index.js | 45.45% | (25 / 55) | 0% | (0 / 13) | 33.33% | (2 / 6) | 48.08% | (25 / 52) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// a trimmed down version of:
// https://github.com/alexcjohnson/world-calendars/blob/master/dist/index.js
module.exports = require('world-calendars/dist/main');
require('world-calendars/dist/plus');
require('world-calendars/dist/calendars/chinese');
require('world-calendars/dist/calendars/coptic');
require('world-calendars/dist/calendars/discworld');
require('world-calendars/dist/calendars/ethiopian');
require('world-calendars/dist/calendars/hebrew');
require('world-calendars/dist/calendars/islamic');
require('world-calendars/dist/calendars/julian');
require('world-calendars/dist/calendars/mayan');
require('world-calendars/dist/calendars/nanakshahi');
require('world-calendars/dist/calendars/nepali');
require('world-calendars/dist/calendars/persian');
require('world-calendars/dist/calendars/taiwan');
require('world-calendars/dist/calendars/thai');
require('world-calendars/dist/calendars/ummalqura');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 3 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var calendars = require('./calendars');
var Lib = require('../../lib');
var constants = require('../../constants/numerical');
var EPOCHJD = constants.EPOCHJD;
var ONEDAY = constants.ONEDAY;
var attributes = {
valType: 'enumerated',
values: Object.keys(calendars.calendars),
role: 'info',
dflt: 'gregorian'
};
var handleDefaults = function(contIn, contOut, attr, dflt) {
var attrs = {};
attrs[attr] = attributes;
return Lib.coerce(contIn, contOut, attrs, attr, dflt);
};
var handleTraceDefaults = function(traceIn, traceOut, coords, layout) {
for(var i = 0; i < coords.length; i++) {
handleDefaults(traceIn, traceOut, coords[i] + 'calendar', layout.calendar);
}
};
// each calendar needs its own default canonical tick. I would love to use
// 2000-01-01 (or even 0000-01-01) for them all but they don't necessarily
// all support either of those dates. Instead I'll use the most significant
// number they *do* support, biased toward the present day.
var CANONICAL_TICK = {
chinese: '2000-01-01',
coptic: '2000-01-01',
discworld: '2000-01-01',
ethiopian: '2000-01-01',
hebrew: '5000-01-01',
islamic: '1000-01-01',
julian: '2000-01-01',
mayan: '5000-01-01',
nanakshahi: '1000-01-01',
nepali: '2000-01-01',
persian: '1000-01-01',
jalali: '1000-01-01',
taiwan: '1000-01-01',
thai: '2000-01-01',
ummalqura: '1400-01-01'
};
// Start on a Sunday - for week ticks
// Discworld and Mayan calendars don't have 7-day weeks but we're going to give them
// 7-day week ticks so start on our Sundays.
// If anyone really cares we can customize the auto tick spacings for these calendars.
var CANONICAL_SUNDAY = {
chinese: '2000-01-02',
coptic: '2000-01-03',
discworld: '2000-01-03',
ethiopian: '2000-01-05',
hebrew: '5000-01-01',
islamic: '1000-01-02',
julian: '2000-01-03',
mayan: '5000-01-01',
nanakshahi: '1000-01-05',
nepali: '2000-01-05',
persian: '1000-01-01',
jalali: '1000-01-01',
taiwan: '1000-01-04',
thai: '2000-01-04',
ummalqura: '1400-01-06'
};
var DFLTRANGE = {
chinese: ['2000-01-01', '2001-01-01'],
coptic: ['1700-01-01', '1701-01-01'],
discworld: ['1800-01-01', '1801-01-01'],
ethiopian: ['2000-01-01', '2001-01-01'],
hebrew: ['5700-01-01', '5701-01-01'],
islamic: ['1400-01-01', '1401-01-01'],
julian: ['2000-01-01', '2001-01-01'],
mayan: ['5200-01-01', '5201-01-01'],
nanakshahi: ['0500-01-01', '0501-01-01'],
nepali: ['2000-01-01', '2001-01-01'],
persian: ['1400-01-01', '1401-01-01'],
jalali: ['1400-01-01', '1401-01-01'],
taiwan: ['0100-01-01', '0101-01-01'],
thai: ['2500-01-01', '2501-01-01'],
ummalqura: ['1400-01-01', '1401-01-01']
};
/*
* convert d3 templates to world-calendars templates, so our users only need
* to know d3's specifiers. Map space padding to no padding, and unknown fields
* to an ugly placeholder
*/
var UNKNOWN = '##';
var d3ToWorldCalendars = {
'd': {'0': 'dd', '-': 'd'}, // 2-digit or unpadded day of month
'e': {'0': 'd', '-': 'd'}, // alternate, always unpadded day of month
'a': {'0': 'D', '-': 'D'}, // short weekday name
'A': {'0': 'DD', '-': 'DD'}, // full weekday name
'j': {'0': 'oo', '-': 'o'}, // 3-digit or unpadded day of the year
'W': {'0': 'ww', '-': 'w'}, // 2-digit or unpadded week of the year (Monday first)
'm': {'0': 'mm', '-': 'm'}, // 2-digit or unpadded month number
'b': {'0': 'M', '-': 'M'}, // short month name
'B': {'0': 'MM', '-': 'MM'}, // full month name
'y': {'0': 'yy', '-': 'yy'}, // 2-digit year (map unpadded to zero-padded)
'Y': {'0': 'yyyy', '-': 'yyyy'}, // 4-digit year (map unpadded to zero-padded)
'U': UNKNOWN, // Sunday-first week of the year
'w': UNKNOWN, // day of the week [0(sunday),6]
// combined format, we replace the date part with the world-calendar version
// and the %X stays there for d3 to handle with time parts
'c': {'0': 'D M d %X yyyy', '-': 'D M d %X yyyy'},
'x': {'0': 'mm/dd/yyyy', '-': 'mm/dd/yyyy'}
};
function worldCalFmt(fmt, x, calendar) {
var dateJD = Math.floor((x + 0.05) / ONEDAY) + EPOCHJD,
cDate = getCal(calendar).fromJD(dateJD),
i = 0,
modifier, directive, directiveLen, directiveObj, replacementPart;
while((i = fmt.indexOf('%', i)) !== -1) {
modifier = fmt.charAt(i + 1);
if(modifier === '0' || modifier === '-' || modifier === '_') {
directiveLen = 3;
directive = fmt.charAt(i + 2);
if(modifier === '_') modifier = '-';
}
else {
directive = modifier;
modifier = '0';
directiveLen = 2;
}
directiveObj = d3ToWorldCalendars[directive];
if(!directiveObj) {
i += directiveLen;
}
else {
// code is recognized as a date part but world-calendars doesn't support it
if(directiveObj === UNKNOWN) replacementPart = UNKNOWN;
// format the cDate according to the translated directive
else replacementPart = cDate.formatDate(directiveObj[modifier]);
fmt = fmt.substr(0, i) + replacementPart + fmt.substr(i + directiveLen);
i += replacementPart.length;
}
}
return fmt;
}
// cache world calendars, so we don't have to reinstantiate
// during each date-time conversion
var allCals = {};
function getCal(calendar) {
var calendarObj = allCals[calendar];
if(calendarObj) return calendarObj;
calendarObj = allCals[calendar] = calendars.instance(calendar);
return calendarObj;
}
function makeAttrs(description) {
return Lib.extendFlat({}, attributes, { description: description });
}
function makeTraceAttrsDescription(coord) {
return 'Sets the calendar system to use with `' + coord + '` date data.';
}
var xAttrs = {
xcalendar: makeAttrs(makeTraceAttrsDescription('x'))
};
var xyAttrs = Lib.extendFlat({}, xAttrs, {
ycalendar: makeAttrs(makeTraceAttrsDescription('y'))
});
var xyzAttrs = Lib.extendFlat({}, xyAttrs, {
zcalendar: makeAttrs(makeTraceAttrsDescription('z'))
});
var axisAttrs = makeAttrs([
'Sets the calendar system to use for `range` and `tick0`',
'if this is a date axis. This does not set the calendar for',
'interpreting data on this axis, that\'s specified in the trace',
'or via the global `layout.calendar`'
].join(' '));
module.exports = {
moduleType: 'component',
name: 'calendars',
schema: {
traces: {
scatter: xyAttrs,
bar: xyAttrs,
heatmap: xyAttrs,
contour: xyAttrs,
histogram: xyAttrs,
histogram2d: xyAttrs,
histogram2dcontour: xyAttrs,
scatter3d: xyzAttrs,
surface: xyzAttrs,
mesh3d: xyzAttrs,
scattergl: xyAttrs,
ohlc: xAttrs,
candlestick: xAttrs
},
layout: {
calendar: makeAttrs([
'Sets the default calendar system to use for interpreting and',
'displaying dates throughout the plot.'
].join(' ')),
'xaxis.calendar': axisAttrs,
'yaxis.calendar': axisAttrs,
'scene.xaxis.calendar': axisAttrs,
'scene.yaxis.calendar': axisAttrs,
'scene.zaxis.calendar': axisAttrs
},
transforms: {
filter: {
valuecalendar: makeAttrs([
'Sets the calendar system to use for `value`, if it is a date.'
].join(' ')),
targetcalendar: makeAttrs([
'Sets the calendar system to use for `target`, if it is an',
'array of dates. If `target` is a string (eg *x*) we use the',
'corresponding trace attribute (eg `xcalendar`) if it exists,',
'even if `targetcalendar` is provided.'
].join(' '))
}
}
},
layoutAttributes: attributes,
handleDefaults: handleDefaults,
handleTraceDefaults: handleTraceDefaults,
CANONICAL_SUNDAY: CANONICAL_SUNDAY,
CANONICAL_TICK: CANONICAL_TICK,
DFLTRANGE: DFLTRANGE,
getCal: getCal,
worldCalFmt: worldCalFmt
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (6 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (6 / 6) | |
| index.js | 20.93% | (18 / 86) | 0% | (0 / 67) | 0% | (0 / 10) | 26.47% | (18 / 68) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// IMPORTANT - default colors should be in hex for compatibility
exports.defaults = [
'#1f77b4', // muted blue
'#ff7f0e', // safety orange
'#2ca02c', // cooked asparagus green
'#d62728', // brick red
'#9467bd', // muted purple
'#8c564b', // chestnut brown
'#e377c2', // raspberry yogurt pink
'#7f7f7f', // middle gray
'#bcbd22', // curry yellow-green
'#17becf' // blue-teal
];
exports.defaultLine = '#444';
exports.lightLine = '#eee';
exports.background = '#fff';
exports.borderLine = '#BEC8D9';
// with axis.color and Color.interp we aren't using lightLine
// itself anymore, instead interpolating between axis.color
// and the background color using tinycolor.mix. lightFraction
// gives back exactly lightLine if the other colors are defaults.
exports.lightFraction = 100 * (0xe - 0x4) / (0xf - 0x4);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var tinycolor = require('tinycolor2');
var isNumeric = require('fast-isnumeric');
var color = module.exports = {};
var colorAttrs = require('./attributes');
color.defaults = colorAttrs.defaults;
var defaultLine = color.defaultLine = colorAttrs.defaultLine;
color.lightLine = colorAttrs.lightLine;
var background = color.background = colorAttrs.background;
/*
* tinyRGB: turn a tinycolor into an rgb string, but
* unlike the built-in tinycolor.toRgbString this never includes alpha
*/
color.tinyRGB = function(tc) {
var c = tc.toRgb();
return 'rgb(' + Math.round(c.r) + ', ' +
Math.round(c.g) + ', ' + Math.round(c.b) + ')';
};
color.rgb = function(cstr) { return color.tinyRGB(tinycolor(cstr)); };
color.opacity = function(cstr) { return cstr ? tinycolor(cstr).getAlpha() : 0; };
color.addOpacity = function(cstr, op) {
var c = tinycolor(cstr).toRgb();
return 'rgba(' + Math.round(c.r) + ', ' +
Math.round(c.g) + ', ' + Math.round(c.b) + ', ' + op + ')';
};
// combine two colors into one apparent color
// if back has transparency or is missing,
// color.background is assumed behind it
color.combine = function(front, back) {
var fc = tinycolor(front).toRgb();
if(fc.a === 1) return tinycolor(front).toRgbString();
var bc = tinycolor(back || background).toRgb(),
bcflat = bc.a === 1 ? bc : {
r: 255 * (1 - bc.a) + bc.r * bc.a,
g: 255 * (1 - bc.a) + bc.g * bc.a,
b: 255 * (1 - bc.a) + bc.b * bc.a
},
fcflat = {
r: bcflat.r * (1 - fc.a) + fc.r * fc.a,
g: bcflat.g * (1 - fc.a) + fc.g * fc.a,
b: bcflat.b * (1 - fc.a) + fc.b * fc.a
};
return tinycolor(fcflat).toRgbString();
};
/*
* Create a color that contrasts with cstr.
*
* If cstr is a dark color, we lighten it; if it's light, we darken.
*
* If lightAmount / darkAmount are used, we adjust by these percentages,
* otherwise we go all the way to white or black.
*/
color.contrast = function(cstr, lightAmount, darkAmount) {
var tc = tinycolor(cstr);
if(tc.getAlpha() !== 1) tc = tinycolor(color.combine(cstr, background));
var newColor = tc.isDark() ?
(lightAmount ? tc.lighten(lightAmount) : background) :
(darkAmount ? tc.darken(darkAmount) : defaultLine);
return newColor.toString();
};
color.stroke = function(s, c) {
var tc = tinycolor(c);
s.style({'stroke': color.tinyRGB(tc), 'stroke-opacity': tc.getAlpha()});
};
color.fill = function(s, c) {
var tc = tinycolor(c);
s.style({
'fill': color.tinyRGB(tc),
'fill-opacity': tc.getAlpha()
});
};
// search container for colors with the deprecated rgb(fractions) format
// and convert them to rgb(0-255 values)
color.clean = function(container) {
if(!container || typeof container !== 'object') return;
var keys = Object.keys(container),
i,
j,
key,
val;
for(i = 0; i < keys.length; i++) {
key = keys[i];
val = container[key];
// only sanitize keys that end in "color" or "colorscale"
if(key.substr(key.length - 5) === 'color') {
if(Array.isArray(val)) {
for(j = 0; j < val.length; j++) val[j] = cleanOne(val[j]);
}
else container[key] = cleanOne(val);
}
else if(key.substr(key.length - 10) === 'colorscale' && Array.isArray(val)) {
// colorscales have the format [[0, color1], [frac, color2], ... [1, colorN]]
for(j = 0; j < val.length; j++) {
if(Array.isArray(val[j])) val[j][1] = cleanOne(val[j][1]);
}
}
// recurse into arrays of objects, and plain objects
else if(Array.isArray(val)) {
var el0 = val[0];
if(!Array.isArray(el0) && el0 && typeof el0 === 'object') {
for(j = 0; j < val.length; j++) color.clean(val[j]);
}
}
else if(val && typeof val === 'object') color.clean(val);
}
};
function cleanOne(val) {
if(isNumeric(val) || typeof val !== 'string') return val;
var valTrim = val.trim();
if(valTrim.substr(0, 3) !== 'rgb') return val;
var match = valTrim.match(/^rgba?\s*\(([^()]*)\)$/);
if(!match) return val;
var parts = match[1].trim().split(/\s*[\s,]\s*/),
rgba = valTrim.charAt(3) === 'a' && parts.length === 4;
if(!rgba && parts.length !== 3) return val;
for(var i = 0; i < parts.length; i++) {
if(!parts[i].length) return val;
parts[i] = Number(parts[i]);
// all parts must be non-negative numbers
if(!(parts[i] >= 0)) return val;
// alpha>1 gets clipped to 1
if(i === 3) {
if(parts[i] > 1) parts[i] = 1;
}
// r, g, b must be < 1 (ie 1 itself is not allowed)
else if(parts[i] >= 1) return val;
}
var rgbStr = Math.round(parts[0] * 255) + ', ' +
Math.round(parts[1] * 255) + ', ' +
Math.round(parts[2] * 255);
if(rgba) return 'rgba(' + rgbStr + ', ' + parts[3] + ')';
return 'rgb(' + rgbStr + ')';
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (4 / 4) | |
| defaults.js | 22.58% | (7 / 31) | 0% | (0 / 6) | 0% | (0 / 2) | 22.58% | (7 / 31) | |
| draw.js | 12% | (24 / 200) | 0% | (0 / 143) | 0% | (0 / 23) | 12.31% | (24 / 195) | |
| has_colorbar.js | 66.67% | (2 / 3) | 100% | (0 / 0) | 0% | (0 / 1) | 66.67% | (2 / 3) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var axesAttrs = require('../../plots/cartesian/layout_attributes');
var fontAttrs = require('../../plots/font_attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
// TODO: only right is supported currently
// orient: {
// valType: 'enumerated',
// role: 'info',
// values: ['left', 'right', 'top', 'bottom'],
// dflt: 'right',
// description: [
// 'Determines which side are the labels on',
// '(so left and right make vertical bars, etc.)'
// ].join(' ')
// },
thicknessmode: {
valType: 'enumerated',
values: ['fraction', 'pixels'],
role: 'style',
dflt: 'pixels',
description: [
'Determines whether this color bar\'s thickness',
'(i.e. the measure in the constant color direction)',
'is set in units of plot *fraction* or in *pixels*.',
'Use `thickness` to set the value.'
].join(' ')
},
thickness: {
valType: 'number',
role: 'style',
min: 0,
dflt: 30,
description: [
'Sets the thickness of the color bar',
'This measure excludes the size of the padding, ticks and labels.'
].join(' ')
},
lenmode: {
valType: 'enumerated',
values: ['fraction', 'pixels'],
role: 'info',
dflt: 'fraction',
description: [
'Determines whether this color bar\'s length',
'(i.e. the measure in the color variation direction)',
'is set in units of plot *fraction* or in *pixels.',
'Use `len` to set the value.'
].join(' ')
},
len: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: [
'Sets the length of the color bar',
'This measure excludes the padding of both ends.',
'That is, the color bar length is this length minus the',
'padding on both ends.'
].join(' ')
},
x: {
valType: 'number',
dflt: 1.02,
min: -2,
max: 3,
role: 'style',
description: [
'Sets the x position of the color bar (in plot fraction).'
].join(' ')
},
xanchor: {
valType: 'enumerated',
values: ['left', 'center', 'right'],
dflt: 'left',
role: 'style',
description: [
'Sets this color bar\'s horizontal position anchor.',
'This anchor binds the `x` position to the *left*, *center*',
'or *right* of the color bar.'
].join(' ')
},
xpad: {
valType: 'number',
role: 'style',
min: 0,
dflt: 10,
description: 'Sets the amount of padding (in px) along the x direction.'
},
y: {
valType: 'number',
role: 'style',
dflt: 0.5,
min: -2,
max: 3,
description: [
'Sets the y position of the color bar (in plot fraction).'
].join(' ')
},
yanchor: {
valType: 'enumerated',
values: ['top', 'middle', 'bottom'],
role: 'style',
dflt: 'middle',
description: [
'Sets this color bar\'s vertical position anchor',
'This anchor binds the `y` position to the *top*, *middle*',
'or *bottom* of the color bar.'
].join(' ')
},
ypad: {
valType: 'number',
role: 'style',
min: 0,
dflt: 10,
description: 'Sets the amount of padding (in px) along the y direction.'
},
// a possible line around the bar itself
outlinecolor: axesAttrs.linecolor,
outlinewidth: axesAttrs.linewidth,
// Should outlinewidth have {dflt: 0} ?
// another possible line outside the padding and tick labels
bordercolor: axesAttrs.linecolor,
borderwidth: {
valType: 'number',
role: 'style',
min: 0,
dflt: 0,
description: [
'Sets the width (in px) or the border enclosing this color bar.'
].join(' ')
},
bgcolor: {
valType: 'color',
role: 'style',
dflt: 'rgba(0,0,0,0)',
description: 'Sets the color of padded area.'
},
// tick and title properties named and function exactly as in axes
tickmode: axesAttrs.tickmode,
nticks: axesAttrs.nticks,
tick0: axesAttrs.tick0,
dtick: axesAttrs.dtick,
tickvals: axesAttrs.tickvals,
ticktext: axesAttrs.ticktext,
ticks: extendFlat({}, axesAttrs.ticks, {dflt: ''}),
ticklen: axesAttrs.ticklen,
tickwidth: axesAttrs.tickwidth,
tickcolor: axesAttrs.tickcolor,
showticklabels: axesAttrs.showticklabels,
tickfont: axesAttrs.tickfont,
tickangle: axesAttrs.tickangle,
tickformat: axesAttrs.tickformat,
tickprefix: axesAttrs.tickprefix,
showtickprefix: axesAttrs.showtickprefix,
ticksuffix: axesAttrs.ticksuffix,
showticksuffix: axesAttrs.showticksuffix,
separatethousands: axesAttrs.separatethousands,
exponentformat: axesAttrs.exponentformat,
showexponent: axesAttrs.showexponent,
title: {
valType: 'string',
role: 'info',
dflt: 'Click to enter colorscale title',
description: 'Sets the title of the color bar.'
},
titlefont: extendFlat({}, fontAttrs, {
description: [
'Sets this color bar\'s title font.'
].join(' ')
}),
titleside: {
valType: 'enumerated',
values: ['right', 'top', 'bottom'],
role: 'style',
dflt: 'top',
description: [
'Determines the location of the colorbar title',
'with respect to the color bar.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
var handleTickMarkDefaults = require('../../plots/cartesian/tick_mark_defaults');
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
var attributes = require('./attributes');
module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
var colorbarOut = containerOut.colorbar = {},
colorbarIn = containerIn.colorbar || {};
function coerce(attr, dflt) {
return Lib.coerce(colorbarIn, colorbarOut, attributes, attr, dflt);
}
var thicknessmode = coerce('thicknessmode');
coerce('thickness', (thicknessmode === 'fraction') ?
30 / (layout.width - layout.margin.l - layout.margin.r) :
30
);
var lenmode = coerce('lenmode');
coerce('len', (lenmode === 'fraction') ?
1 :
layout.height - layout.margin.t - layout.margin.b
);
coerce('x');
coerce('xanchor');
coerce('xpad');
coerce('y');
coerce('yanchor');
coerce('ypad');
Lib.noneOrAll(colorbarIn, colorbarOut, ['x', 'y']);
coerce('outlinecolor');
coerce('outlinewidth');
coerce('bordercolor');
coerce('borderwidth');
coerce('bgcolor');
handleTickValueDefaults(colorbarIn, colorbarOut, coerce, 'linear');
handleTickLabelDefaults(colorbarIn, colorbarOut, coerce, 'linear',
{outerTicks: false, font: layout.font, noHover: true});
handleTickMarkDefaults(colorbarIn, colorbarOut, coerce, 'linear',
{outerTicks: false, font: layout.font, noHover: true});
coerce('title');
Lib.coerceFont(coerce, 'titlefont', layout.font);
coerce('titleside');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var tinycolor = require('tinycolor2');
var Plotly = require('../../plotly');
var Plots = require('../../plots/plots');
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var dragElement = require('../dragelement');
var Lib = require('../../lib');
var extendFlat = require('../../lib/extend').extendFlat;
var setCursor = require('../../lib/setcursor');
var Drawing = require('../drawing');
var Color = require('../color');
var Titles = require('../titles');
var handleAxisDefaults = require('../../plots/cartesian/axis_defaults');
var handleAxisPositionDefaults = require('../../plots/cartesian/position_defaults');
var axisLayoutAttrs = require('../../plots/cartesian/layout_attributes');
var attributes = require('./attributes');
module.exports = function draw(gd, id) {
// opts: options object, containing everything from attributes
// plus a few others that are the equivalent of the colorbar "data"
var opts = {};
Object.keys(attributes).forEach(function(k) {
opts[k] = null;
});
// fillcolor can be a d3 scale, domain is z values, range is colors
// or leave it out for no fill,
// or set to a string constant for single-color fill
opts.fillcolor = null;
// line.color has the same options as fillcolor
opts.line = {color: null, width: null, dash: null};
// levels of lines to draw.
// note that this DOES NOT determine the extent of the bar
// that's given by the domain of fillcolor
// (or line.color if no fillcolor domain)
opts.levels = {start: null, end: null, size: null};
// separate fill levels (for example, heatmap coloring of a
// contour map) if this is omitted, fillcolors will be
// evaluated halfway between levels
opts.filllevels = null;
function component() {
var fullLayout = gd._fullLayout,
gs = fullLayout._size;
if((typeof opts.fillcolor !== 'function') &&
(typeof opts.line.color !== 'function')) {
fullLayout._infolayer.selectAll('g.' + id).remove();
return;
}
var zrange = d3.extent(((typeof opts.fillcolor === 'function') ?
opts.fillcolor : opts.line.color).domain()),
linelevels = [],
filllevels = [],
l,
linecolormap = typeof opts.line.color === 'function' ?
opts.line.color : function() { return opts.line.color; },
fillcolormap = typeof opts.fillcolor === 'function' ?
opts.fillcolor : function() { return opts.fillcolor; };
var l0 = opts.levels.end + opts.levels.size / 100,
ls = opts.levels.size,
zr0 = (1.001 * zrange[0] - 0.001 * zrange[1]),
zr1 = (1.001 * zrange[1] - 0.001 * zrange[0]);
for(l = opts.levels.start; (l - l0) * ls < 0; l += ls) {
if(l > zr0 && l < zr1) linelevels.push(l);
}
if(typeof opts.fillcolor === 'function') {
if(opts.filllevels) {
l0 = opts.filllevels.end + opts.filllevels.size / 100;
ls = opts.filllevels.size;
for(l = opts.filllevels.start; (l - l0) * ls < 0; l += ls) {
if(l > zrange[0] && l < zrange[1]) filllevels.push(l);
}
}
else {
filllevels = linelevels.map(function(v) {
return v - opts.levels.size / 2;
});
filllevels.push(filllevels[filllevels.length - 1] +
opts.levels.size);
}
}
else if(opts.fillcolor && typeof opts.fillcolor === 'string') {
// doesn't matter what this value is, with a single value
// we'll make a single fill rect covering the whole bar
filllevels = [0];
}
if(opts.levels.size < 0) {
linelevels.reverse();
filllevels.reverse();
}
// now make a Plotly Axes object to scale with and draw ticks
// TODO: does not support orientation other than right
// we calculate pixel sizes based on the specified graph size,
// not the actual (in case something pushed the margins around)
// which is a little odd but avoids an odd iterative effect
// when the colorbar itself is pushing the margins.
// but then the fractional size is calculated based on the
// actual graph size, so that the axes will size correctly.
var originalPlotHeight = fullLayout.height - fullLayout.margin.t - fullLayout.margin.b,
originalPlotWidth = fullLayout.width - fullLayout.margin.l - fullLayout.margin.r,
thickPx = Math.round(opts.thickness *
(opts.thicknessmode === 'fraction' ? originalPlotWidth : 1)),
thickFrac = thickPx / gs.w,
lenPx = Math.round(opts.len *
(opts.lenmode === 'fraction' ? originalPlotHeight : 1)),
lenFrac = lenPx / gs.h,
xpadFrac = opts.xpad / gs.w,
yExtraPx = (opts.borderwidth + opts.outlinewidth) / 2,
ypadFrac = opts.ypad / gs.h,
// x positioning: do it initially just for left anchor,
// then fix at the end (since we don't know the width yet)
xLeft = Math.round(opts.x * gs.w + opts.xpad),
// for dragging... this is getting a little muddled...
xLeftFrac = opts.x - thickFrac *
({middle: 0.5, right: 1}[opts.xanchor]||0),
// y positioning we can do correctly from the start
yBottomFrac = opts.y + lenFrac *
(({top: -0.5, bottom: 0.5}[opts.yanchor] || 0) - 0.5),
yBottomPx = Math.round(gs.h * (1 - yBottomFrac)),
yTopPx = yBottomPx - lenPx,
titleEl,
cbAxisIn = {
type: 'linear',
range: zrange,
tickmode: opts.tickmode,
nticks: opts.nticks,
tick0: opts.tick0,
dtick: opts.dtick,
tickvals: opts.tickvals,
ticktext: opts.ticktext,
ticks: opts.ticks,
ticklen: opts.ticklen,
tickwidth: opts.tickwidth,
tickcolor: opts.tickcolor,
showticklabels: opts.showticklabels,
tickfont: opts.tickfont,
tickangle: opts.tickangle,
tickformat: opts.tickformat,
exponentformat: opts.exponentformat,
separatethousands: opts.separatethousands,
showexponent: opts.showexponent,
showtickprefix: opts.showtickprefix,
tickprefix: opts.tickprefix,
showticksuffix: opts.showticksuffix,
ticksuffix: opts.ticksuffix,
title: opts.title,
titlefont: opts.titlefont,
anchor: 'free',
position: 1
},
cbAxisOut = {
type: 'linear',
_id: 'y' + id
},
axisOptions = {
letter: 'y',
font: fullLayout.font,
noHover: true,
calendar: fullLayout.calendar // not really necessary (yet?)
};
// Coerce w.r.t. Axes layoutAttributes:
// re-use axes.js logic without updating _fullData
function coerce(attr, dflt) {
return Lib.coerce(cbAxisIn, cbAxisOut, axisLayoutAttrs, attr, dflt);
}
// Prepare the Plotly axis object
handleAxisDefaults(cbAxisIn, cbAxisOut, coerce, axisOptions, fullLayout);
handleAxisPositionDefaults(cbAxisIn, cbAxisOut, coerce, axisOptions);
// position can't go in through supplyDefaults
// because that restricts it to [0,1]
cbAxisOut.position = opts.x + xpadFrac + thickFrac;
// save for other callers to access this axis
component.axis = cbAxisOut;
if(['top', 'bottom'].indexOf(opts.titleside) !== -1) {
cbAxisOut.titleside = opts.titleside;
cbAxisOut.titlex = opts.x + xpadFrac;
cbAxisOut.titley = yBottomFrac +
(opts.titleside === 'top' ? lenFrac - ypadFrac : ypadFrac);
}
if(opts.line.color && opts.tickmode === 'auto') {
cbAxisOut.tickmode = 'linear';
cbAxisOut.tick0 = opts.levels.start;
var dtick = opts.levels.size;
// expand if too many contours, so we don't get too many ticks
var autoNtick = Lib.constrain(
(yBottomPx - yTopPx) / 50, 4, 15) + 1,
dtFactor = (zrange[1] - zrange[0]) /
((opts.nticks || autoNtick) * dtick);
if(dtFactor > 1) {
var dtexp = Math.pow(10, Math.floor(
Math.log(dtFactor) / Math.LN10));
dtick *= dtexp * Lib.roundUp(dtFactor / dtexp, [2, 5, 10]);
// if the contours are at round multiples, reset tick0
// so they're still at round multiples. Otherwise,
// keep the first label on the first contour level
if((Math.abs(opts.levels.start) /
opts.levels.size + 1e-6) % 1 < 2e-6) {
cbAxisOut.tick0 = 0;
}
}
cbAxisOut.dtick = dtick;
}
// set domain after init, because we may want to
// allow it outside [0,1]
cbAxisOut.domain = [
yBottomFrac + ypadFrac,
yBottomFrac + lenFrac - ypadFrac
];
cbAxisOut.setScale();
// now draw the elements
var container = fullLayout._infolayer.selectAll('g.' + id).data([0]);
container.enter().append('g').classed(id, true)
.each(function() {
var s = d3.select(this);
s.append('rect').classed('cbbg', true);
s.append('g').classed('cbfills', true);
s.append('g').classed('cblines', true);
s.append('g').classed('cbaxis', true).classed('crisp', true);
s.append('g').classed('cbtitleunshift', true)
.append('g').classed('cbtitle', true);
s.append('rect').classed('cboutline', true);
s.select('.cbtitle').datum(0);
});
container.attr('transform', 'translate(' + Math.round(gs.l) +
',' + Math.round(gs.t) + ')');
// TODO: this opposite transform is a hack until we make it
// more rational which items get this offset
var titleCont = container.select('.cbtitleunshift')
.attr('transform', 'translate(-' +
Math.round(gs.l) + ',-' +
Math.round(gs.t) + ')');
cbAxisOut._axislayer = container.select('.cbaxis');
var titleHeight = 0;
if(['top', 'bottom'].indexOf(opts.titleside) !== -1) {
// draw the title so we know how much room it needs
// when we squish the axis. This one only applies to
// top or bottom titles, not right side.
var x = gs.l + (opts.x + xpadFrac) * gs.w,
fontSize = cbAxisOut.titlefont.size,
y;
if(opts.titleside === 'top') {
y = (1 - (yBottomFrac + lenFrac - ypadFrac)) * gs.h +
gs.t + 3 + fontSize * 0.75;
}
else {
y = (1 - (yBottomFrac + ypadFrac)) * gs.h +
gs.t - 3 - fontSize * 0.25;
}
drawTitle(cbAxisOut._id + 'title', {
attributes: {x: x, y: y, 'text-anchor': 'start'}
});
}
function drawAxis() {
if(['top', 'bottom'].indexOf(opts.titleside) !== -1) {
// squish the axis top to make room for the title
var titleGroup = container.select('.cbtitle'),
titleText = titleGroup.select('text'),
titleTrans =
[-opts.outlinewidth / 2, opts.outlinewidth / 2],
mathJaxNode = titleGroup
.select('.h' + cbAxisOut._id + 'title-math-group')
.node(),
lineSize = 15.6;
if(titleText.node()) {
lineSize =
parseInt(titleText.style('font-size'), 10) * 1.3;
}
if(mathJaxNode) {
titleHeight = Drawing.bBox(mathJaxNode).height;
if(titleHeight > lineSize) {
// not entirely sure how mathjax is doing
// vertical alignment, but this seems to work.
titleTrans[1] -= (titleHeight - lineSize) / 2;
}
}
else if(titleText.node() &&
!titleText.classed('js-placeholder')) {
titleHeight = Drawing.bBox(
titleGroup.node()).height;
}
if(titleHeight) {
// buffer btwn colorbar and title
// TODO: configurable
titleHeight += 5;
if(opts.titleside === 'top') {
cbAxisOut.domain[1] -= titleHeight / gs.h;
titleTrans[1] *= -1;
}
else {
cbAxisOut.domain[0] += titleHeight / gs.h;
var nlines = Math.max(1,
titleText.selectAll('tspan.line').size());
titleTrans[1] += (1 - nlines) * lineSize;
}
titleGroup.attr('transform',
'translate(' + titleTrans + ')');
cbAxisOut.setScale();
}
}
container.selectAll('.cbfills,.cblines,.cbaxis')
.attr('transform', 'translate(0,' +
Math.round(gs.h * (1 - cbAxisOut.domain[1])) + ')');
var fills = container.select('.cbfills')
.selectAll('rect.cbfill')
.data(filllevels);
fills.enter().append('rect')
.classed('cbfill', true)
.style('stroke', 'none');
fills.exit().remove();
fills.each(function(d, i) {
var z = [
(i === 0) ? zrange[0] :
(filllevels[i] + filllevels[i - 1]) / 2,
(i === filllevels.length - 1) ? zrange[1] :
(filllevels[i] + filllevels[i + 1]) / 2
]
.map(cbAxisOut.c2p)
.map(Math.round);
// offset the side adjoining the next rectangle so they
// overlap, to prevent antialiasing gaps
if(i !== filllevels.length - 1) {
z[1] += (z[1] > z[0]) ? 1 : -1;
}
// Tinycolor can't handle exponents and
// at this scale, removing it makes no difference.
var colorString = fillcolormap(d).replace('e-', ''),
opaqueColor = tinycolor(colorString).toHexString();
// Colorbar cannot currently support opacities so we
// use an opaque fill even when alpha channels present
d3.select(this).attr({
x: xLeft,
width: Math.max(thickPx, 2),
y: d3.min(z),
height: Math.max(d3.max(z) - d3.min(z), 2),
fill: opaqueColor
});
});
var lines = container.select('.cblines')
.selectAll('path.cbline')
.data(opts.line.color && opts.line.width ?
linelevels : []);
lines.enter().append('path')
.classed('cbline', true);
lines.exit().remove();
lines.each(function(d) {
d3.select(this)
.attr('d', 'M' + xLeft + ',' +
(Math.round(cbAxisOut.c2p(d)) + (opts.line.width / 2) % 1) +
'h' + thickPx)
.call(Drawing.lineGroupStyle,
opts.line.width, linecolormap(d), opts.line.dash);
});
// force full redraw of labels and ticks
cbAxisOut._axislayer.selectAll('g.' + cbAxisOut._id + 'tick,path')
.remove();
cbAxisOut._pos = xLeft + thickPx +
(opts.outlinewidth||0) / 2 - (opts.ticks === 'outside' ? 1 : 0);
cbAxisOut.side = 'right';
// separate out axis and title drawing,
// so we don't need such complicated logic in Titles.draw
// if title is on the top or bottom, we've already drawn it
// this title call only handles side=right
return Lib.syncOrAsync([
function() {
return Axes.doTicks(gd, cbAxisOut, true);
},
function() {
if(['top', 'bottom'].indexOf(opts.titleside) === -1) {
var fontSize = cbAxisOut.titlefont.size,
y = cbAxisOut._offset + cbAxisOut._length / 2,
x = gs.l + (cbAxisOut.position || 0) * gs.w + ((cbAxisOut.side === 'right') ?
10 + fontSize * ((cbAxisOut.showticklabels ? 1 : 0.5)) :
-10 - fontSize * ((cbAxisOut.showticklabels ? 0.5 : 0)));
// the 'h' + is a hack to get around the fact that
// convertToTspans rotates any 'y...' class by 90 degrees.
// TODO: find a better way to control this.
drawTitle('h' + cbAxisOut._id + 'title', {
avoid: {
selection: d3.select(gd).selectAll('g.' + cbAxisOut._id + 'tick'),
side: opts.titleside,
offsetLeft: gs.l,
offsetTop: gs.t,
maxShift: fullLayout.width
},
attributes: {x: x, y: y, 'text-anchor': 'middle'},
transform: {rotate: '-90', offset: 0}
});
}
}]);
}
function drawTitle(titleClass, titleOpts) {
var trace = getTrace(),
propName;
if(Registry.traceIs(trace, 'markerColorscale')) {
propName = 'marker.colorbar.title';
}
else propName = 'colorbar.title';
var dfltTitleOpts = {
propContainer: cbAxisOut,
propName: propName,
traceIndex: trace.index,
dfltName: 'colorscale',
containerGroup: container.select('.cbtitle')
};
// this class-to-rotate thing with convertToTspans is
// getting hackier and hackier... delete groups with the
// wrong class (in case earlier the colorbar was drawn on
// a different side, I think?)
var otherClass = titleClass.charAt(0) === 'h' ?
titleClass.substr(1) : ('h' + titleClass);
container.selectAll('.' + otherClass + ',.' + otherClass + '-math-group')
.remove();
Titles.draw(gd, titleClass,
extendFlat(dfltTitleOpts, titleOpts || {}));
}
function positionCB() {
// wait for the axis & title to finish rendering before
// continuing positioning
// TODO: why are we redrawing multiple times now with this?
// I guess autoMargin doesn't like being post-promise?
var innerWidth = thickPx + opts.outlinewidth / 2 +
Drawing.bBox(cbAxisOut._axislayer.node()).width;
titleEl = titleCont.select('text');
if(titleEl.node() && !titleEl.classed('js-placeholder')) {
var mathJaxNode = titleCont
.select('.h' + cbAxisOut._id + 'title-math-group')
.node(),
titleWidth;
if(mathJaxNode &&
['top', 'bottom'].indexOf(opts.titleside) !== -1) {
titleWidth = Drawing.bBox(mathJaxNode).width;
}
else {
// note: the formula below works for all titlesides,
// (except for top/bottom mathjax, above)
// but the weird gs.l is because the titleunshift
// transform gets removed by Drawing.bBox
titleWidth =
Drawing.bBox(titleCont.node()).right -
xLeft - gs.l;
}
innerWidth = Math.max(innerWidth, titleWidth);
}
var outerwidth = 2 * opts.xpad + innerWidth +
opts.borderwidth + opts.outlinewidth / 2,
outerheight = yBottomPx - yTopPx;
container.select('.cbbg').attr({
x: xLeft - opts.xpad -
(opts.borderwidth + opts.outlinewidth) / 2,
y: yTopPx - yExtraPx,
width: Math.max(outerwidth, 2),
height: Math.max(outerheight + 2 * yExtraPx, 2)
})
.call(Color.fill, opts.bgcolor)
.call(Color.stroke, opts.bordercolor)
.style({'stroke-width': opts.borderwidth});
container.selectAll('.cboutline').attr({
x: xLeft,
y: yTopPx + opts.ypad +
(opts.titleside === 'top' ? titleHeight : 0),
width: Math.max(thickPx, 2),
height: Math.max(outerheight - 2 * opts.ypad - titleHeight, 2)
})
.call(Color.stroke, opts.outlinecolor)
.style({
fill: 'None',
'stroke-width': opts.outlinewidth
});
// fix positioning for xanchor!='left'
var xoffset = ({center: 0.5, right: 1}[opts.xanchor] || 0) *
outerwidth;
container.attr('transform',
'translate(' + (gs.l - xoffset) + ',' + gs.t + ')');
// auto margin adjustment
Plots.autoMargin(gd, id, {
x: opts.x,
y: opts.y,
l: outerwidth * ({right: 1, center: 0.5}[opts.xanchor] || 0),
r: outerwidth * ({left: 1, center: 0.5}[opts.xanchor] || 0),
t: outerheight * ({bottom: 1, middle: 0.5}[opts.yanchor] || 0),
b: outerheight * ({top: 1, middle: 0.5}[opts.yanchor] || 0)
});
}
var cbDone = Lib.syncOrAsync([
Plots.previousPromises,
drawAxis,
Plots.previousPromises,
positionCB
], gd);
if(cbDone && cbDone.then) (gd._promises || []).push(cbDone);
// dragging...
if(gd._context.editable) {
var t0,
xf,
yf;
dragElement.init({
element: container.node(),
prepFn: function() {
t0 = container.attr('transform');
setCursor(container);
},
moveFn: function(dx, dy) {
container.attr('transform',
t0 + ' ' + 'translate(' + dx + ',' + dy + ')');
xf = dragElement.align(xLeftFrac + (dx / gs.w), thickFrac,
0, 1, opts.xanchor);
yf = dragElement.align(yBottomFrac - (dy / gs.h), lenFrac,
0, 1, opts.yanchor);
var csr = dragElement.getCursor(xf, yf,
opts.xanchor, opts.yanchor);
setCursor(container, csr);
},
doneFn: function(dragged) {
setCursor(container);
if(dragged && xf !== undefined && yf !== undefined) {
Plotly.restyle(gd,
{'colorbar.x': xf, 'colorbar.y': yf},
getTrace().index);
}
}
});
}
return cbDone;
}
function getTrace() {
var idNum = id.substr(2),
i,
trace;
for(i = 0; i < gd._fullData.length; i++) {
trace = gd._fullData[i];
if(trace.uid === idNum) return trace;
}
}
// setter/getters for every item defined in opts
Object.keys(opts).forEach(function(name) {
component[name] = function(v) {
// getter
if(!arguments.length) return opts[name];
// setter - for multi-part properties,
// set only the parts that are provided
opts[name] = Lib.isPlainObject(opts[name]) ?
Lib.extendFlat(opts[name], v) :
v;
return component;
};
});
// or use .options to set multiple options at once via a dictionary
component.options = function(o) {
Object.keys(o).forEach(function(name) {
// in case something random comes through
// that's not an option, ignore it
if(typeof component[name] === 'function') {
component[name](o[name]);
}
});
return component;
};
component._opts = opts;
return component;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
module.exports = function hasColorbar(container) {
return Lib.isPlainObject(container.colorbar);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| calc.js | 12.12% | (4 / 33) | 0% | (0 / 23) | 0% | (0 / 1) | 13.33% | (4 / 30) | |
| color_attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (1 / 1) | 100% | (5 / 5) | |
| default_scale.js | 100% | (2 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (2 / 2) | |
| defaults.js | 25% | (7 / 28) | 0% | (0 / 21) | 0% | (0 / 1) | 30.43% | (7 / 23) | |
| extract_scale.js | 14.29% | (1 / 7) | 100% | (0 / 0) | 0% | (0 / 1) | 14.29% | (1 / 7) | |
| flip_scale.js | 16.67% | (1 / 6) | 100% | (0 / 0) | 0% | (0 / 1) | 16.67% | (1 / 6) | |
| get_scale.js | 26.32% | (5 / 19) | 0% | (0 / 12) | 0% | (0 / 2) | 33.33% | (5 / 15) | |
| has_colorscale.js | 36.36% | (4 / 11) | 0% | (0 / 15) | 0% | (0 / 1) | 36.36% | (4 / 11) | |
| index.js | 100% | (11 / 11) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (11 / 11) | |
| is_valid_scale.js | 50% | (3 / 6) | 0% | (0 / 2) | 0% | (0 / 1) | 60% | (3 / 5) | |
| is_valid_scale_array.js | 13.33% | (2 / 15) | 0% | (0 / 17) | 0% | (0 / 1) | 16.67% | (2 / 12) | |
| make_color_scale_func.js | 16.22% | (6 / 37) | 0% | (0 / 18) | 0% | (0 / 6) | 18.75% | (6 / 32) | |
| scales.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
zauto: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines the whether or not the color domain is computed',
'with respect to the input data.'
].join(' ')
},
zmin: {
valType: 'number',
role: 'info',
dflt: null,
description: 'Sets the lower bound of color domain.'
},
zmax: {
valType: 'number',
role: 'info',
dflt: null,
description: 'Sets the upper bound of color domain.'
},
colorscale: {
valType: 'colorscale',
role: 'style',
description: [
'Sets the colorscale.',
'The colorscale must be an array containing',
'arrays mapping a normalized value to an',
'rgb, rgba, hex, hsl, hsv, or named color string.',
'At minimum, a mapping for the lowest (0) and highest (1)',
'values are required. For example,',
'`[[0, \'rgb(0,0,255)\', [1, \'rgb(255,0,0)\']]`.',
'To control the bounds of the colorscale in z space,',
'use zmin and zmax'
].join(' ')
},
autocolorscale: {
valType: 'boolean',
role: 'style',
dflt: true, // gets overrode in 'heatmap' & 'surface' for backwards comp.
description: [
'Determines whether or not the colorscale is picked using the sign of',
'the input z values.'
].join(' ')
},
reversescale: {
valType: 'boolean',
role: 'style',
dflt: false,
description: 'Reverses the colorscale.'
},
showscale: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not a colorbar is displayed for this trace.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var scales = require('./scales');
var flipScale = require('./flip_scale');
module.exports = function calc(trace, vals, containerStr, cLetter) {
var container, inputContainer;
if(containerStr) {
container = Lib.nestedProperty(trace, containerStr).get();
inputContainer = Lib.nestedProperty(trace._input, containerStr).get();
}
else {
container = trace;
inputContainer = trace._input;
}
var autoAttr = cLetter + 'auto',
minAttr = cLetter + 'min',
maxAttr = cLetter + 'max',
auto = container[autoAttr],
min = container[minAttr],
max = container[maxAttr],
scl = container.colorscale;
if(auto !== false || min === undefined) {
min = Lib.aggNums(Math.min, null, vals);
}
if(auto !== false || max === undefined) {
max = Lib.aggNums(Math.max, null, vals);
}
if(min === max) {
min -= 0.5;
max += 0.5;
}
container[minAttr] = min;
container[maxAttr] = max;
inputContainer[minAttr] = min;
inputContainer[maxAttr] = max;
/*
* If auto was explicitly false but min or max was missing,
* we filled in the missing piece here but later the trace does
* not look auto.
* Otherwise make sure the trace still looks auto as far as later
* changes are concerned.
*/
inputContainer[autoAttr] = (auto !== false ||
(min === undefined && max === undefined));
if(container.autocolorscale) {
if(min * max < 0) scl = scales.RdBu;
else if(min >= 0) scl = scales.Reds;
else scl = scales.Blues;
// reversescale is handled at the containerOut level
inputContainer.colorscale = scl;
if(container.reversescale) scl = flipScale(scl);
container.colorscale = scl;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 1 1 1 1 62 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorScaleAttributes = require('./attributes');
var extendDeep = require('../../lib/extend').extendDeep;
var palettes = require('./scales.js');
module.exports = function makeColorScaleAttributes(context) {
return {
color: {
valType: 'color',
arrayOk: true,
role: 'style',
description: [
'Sets the ', context, ' color. It accepts either a specific color',
' or an array of numbers that are mapped to the colorscale',
' relative to the max and min values of the array or relative to',
' `cmin` and `cmax` if set.'
].join('')
},
colorscale: extendDeep({}, colorScaleAttributes.colorscale, {
description: [
'Sets the colorscale and only has an effect',
' if `', context, '.color` is set to a numerical array.',
' The colorscale must be an array containing',
' arrays mapping a normalized value to an',
' rgb, rgba, hex, hsl, hsv, or named color string.',
' At minimum, a mapping for the lowest (0) and highest (1)',
' values are required. For example,',
' `[[0, \'rgb(0,0,255)\', [1, \'rgb(255,0,0)\']]`.',
' To control the bounds of the colorscale in color space,',
' use `', context, '.cmin` and `', context, '.cmax`.',
' Alternatively, `colorscale` may be a palette name string',
' of the following list: '
].join('').concat(Object.keys(palettes).join(', '))
}),
cauto: extendDeep({}, colorScaleAttributes.zauto, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array',
' and `cmin`, `cmax` are set by the user. In this case,',
' it controls whether the range of colors in `colorscale` is mapped to',
' the range of values in the `color` array (`cauto: true`), or the `cmin`/`cmax`',
' values (`cauto: false`).',
' Defaults to `false` when `cmin`, `cmax` are set by the user.'
].join('')
}),
cmax: extendDeep({}, colorScaleAttributes.zmax, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Sets the upper bound of the color domain.',
' Value should be associated to the `', context, '.color` array index,',
' and if set, `', context, '.cmin` must be set as well.'
].join('')
}),
cmin: extendDeep({}, colorScaleAttributes.zmin, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Sets the lower bound of the color domain.',
' Value should be associated to the `', context, '.color` array index,',
' and if set, `', context, '.cmax` must be set as well.'
].join('')
}),
autocolorscale: extendDeep({}, colorScaleAttributes.autocolorscale, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Determines whether the colorscale is a default palette (`autocolorscale: true`)',
' or the palette determined by `', context, '.colorscale`.',
' In case `colorscale` is unspecified or `autocolorscale` is true, the default ',
' palette will be chosen according to whether numbers in the `color` array are',
' all positive, all negative or mixed.'
].join('')
}),
reversescale: extendDeep({}, colorScaleAttributes.reversescale, {
description: [
'Has an effect only if `', context, '.color` is set to a numerical array.',
' Reverses the color mapping if true (`cmin` will correspond to the last color',
' in the array and `cmax` will correspond to the first color).'
].join('')
})
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scales = require('./scales');
module.exports = scales.RdBu;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var hasColorbar = require('../colorbar/has_colorbar');
var colorbarDefaults = require('../colorbar/defaults');
var isValidScale = require('./is_valid_scale');
var flipScale = require('./flip_scale');
module.exports = function colorScaleDefaults(traceIn, traceOut, layout, coerce, opts) {
var prefix = opts.prefix,
cLetter = opts.cLetter,
containerStr = prefix.slice(0, prefix.length - 1),
containerIn = prefix ?
Lib.nestedProperty(traceIn, containerStr).get() || {} :
traceIn,
containerOut = prefix ?
Lib.nestedProperty(traceOut, containerStr).get() || {} :
traceOut,
minIn = containerIn[cLetter + 'min'],
maxIn = containerIn[cLetter + 'max'],
sclIn = containerIn.colorscale;
var validMinMax = isNumeric(minIn) && isNumeric(maxIn) && (minIn < maxIn);
coerce(prefix + cLetter + 'auto', !validMinMax);
coerce(prefix + cLetter + 'min');
coerce(prefix + cLetter + 'max');
// handles both the trace case (autocolorscale is false by default) and
// the marker and marker.line case (autocolorscale is true by default)
var autoColorscaleDftl;
if(sclIn !== undefined) autoColorscaleDftl = !isValidScale(sclIn);
coerce(prefix + 'autocolorscale', autoColorscaleDftl);
var sclOut = coerce(prefix + 'colorscale');
// reversescale is handled at the containerOut level
var reverseScale = coerce(prefix + 'reversescale');
if(reverseScale) containerOut.colorscale = flipScale(sclOut);
// ... until Scatter.colorbar can handle marker line colorbars
if(prefix === 'marker.line.') return;
// handle both the trace case where the dflt is listed in attributes and
// the marker case where the dflt is determined by hasColorbar
var showScaleDftl;
if(prefix) showScaleDftl = hasColorbar(containerIn);
var showScale = coerce(prefix + 'showscale', showScaleDftl);
if(showScale) colorbarDefaults(containerIn, containerOut, layout);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Extract colorscale into numeric domain and color range.
*
* @param {array} scl colorscale array of arrays
* @param {number} cmin minimum color value (used to clamp scale)
* @param {number} cmax maximum color value (used to clamp scale)
*/
module.exports = function extractScale(scl, cmin, cmax) {
var N = scl.length,
domain = new Array(N),
range = new Array(N);
for(var i = 0; i < N; i++) {
var si = scl[i];
domain[i] = cmin + si[0] * (cmax - cmin);
range[i] = si[1];
}
return {
domain: domain,
range: range
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function flipScale(scl) { var N = scl.length, sclNew = new Array(N), si; for(var i = N - 1, j = 0; i >= 0; i--, j++) { si = scl[i]; sclNew[j] = [1 - si[0], si[1]]; } return sclNew; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scales = require('./scales');
var defaultScale = require('./default_scale');
var isValidScaleArray = require('./is_valid_scale_array');
module.exports = function getScale(scl, dflt) {
if(!dflt) dflt = defaultScale;
if(!scl) return dflt;
function parseScale() {
try {
scl = scales[scl] || JSON.parse(scl);
}
catch(e) {
scl = dflt;
}
}
if(typeof scl === 'string') {
parseScale();
// occasionally scl is double-JSON encoded...
if(typeof scl === 'string') parseScale();
}
if(!isValidScaleArray(scl)) return dflt;
return scl;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var isValidScale = require('./is_valid_scale');
module.exports = function hasColorscale(trace, containerStr) {
var container = containerStr ?
Lib.nestedProperty(trace, containerStr).get() || {} :
trace,
color = container.color,
isArrayWithOneNumber = false;
if(Array.isArray(color)) {
for(var i = 0; i < color.length; i++) {
if(isNumeric(color[i])) {
isArrayWithOneNumber = true;
break;
}
}
}
return (
Lib.isPlainObject(container) && (
isArrayWithOneNumber ||
container.showscale === true ||
(isNumeric(container.cmin) && isNumeric(container.cmax)) ||
isValidScale(container.colorscale) ||
Lib.isPlainObject(container.colorbar)
)
);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
exports.scales = require('./scales');
exports.defaultScale = require('./default_scale');
exports.attributes = require('./attributes');
exports.handleDefaults = require('./defaults');
exports.calc = require('./calc');
exports.hasColorscale = require('./has_colorscale');
exports.isValidScale = require('./is_valid_scale');
exports.getScale = require('./get_scale');
exports.flipScale = require('./flip_scale');
exports.extractScale = require('./extract_scale');
exports.makeColorScaleFunc = require('./make_color_scale_func');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scales = require('./scales');
var isValidScaleArray = require('./is_valid_scale_array');
module.exports = function isValidScale(scl) {
if(scales[scl] !== undefined) return true;
else return isValidScaleArray(scl);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var tinycolor = require('tinycolor2');
module.exports = function isValidScaleArray(scl) {
var highestVal = 0;
if(!Array.isArray(scl) || scl.length < 2) return false;
if(!scl[0] || !scl[scl.length - 1]) return false;
if(+scl[0][0] !== 0 || +scl[scl.length - 1][0] !== 1) return false;
for(var i = 0; i < scl.length; i++) {
var si = scl[i];
if(si.length !== 2 || +si[0] < highestVal || !tinycolor(si[1]).isValid()) {
return false;
}
highestVal = +si[0];
}
return true;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var tinycolor = require('tinycolor2');
var isNumeric = require('fast-isnumeric');
var Color = require('../color');
/**
* General colorscale function generator.
*
* @param {object} specs output of Colorscale.extractScale or precomputed domain, range.
* - domain {array}
* - range {array}
*
* @param {object} opts
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings
*
* @return {function}
*/
module.exports = function makeColorScaleFunc(specs, opts) {
opts = opts || {};
var domain = specs.domain,
range = specs.range,
N = range.length,
_range = new Array(N);
for(var i = 0; i < N; i++) {
var rgba = tinycolor(range[i]).toRgb();
_range[i] = [rgba.r, rgba.g, rgba.b, rgba.a];
}
var _sclFunc = d3.scale.linear()
.domain(domain)
.range(_range)
.clamp(true);
var noNumericCheck = opts.noNumericCheck,
returnArray = opts.returnArray,
sclFunc;
if(noNumericCheck && returnArray) {
sclFunc = _sclFunc;
}
else if(noNumericCheck) {
sclFunc = function(v) {
return colorArray2rbga(_sclFunc(v));
};
}
else if(returnArray) {
sclFunc = function(v) {
if(isNumeric(v)) return _sclFunc(v);
else if(tinycolor(v).isValid()) return v;
else return Color.defaultLine;
};
}
else {
sclFunc = function(v) {
if(isNumeric(v)) return colorArray2rbga(_sclFunc(v));
else if(tinycolor(v).isValid()) return v;
else return Color.defaultLine;
};
}
// colorbar draw looks into the d3 scale closure for domain and range
sclFunc.domain = _sclFunc.domain;
sclFunc.range = function() { return range; };
return sclFunc;
};
function colorArray2rbga(colorArray) {
var colorObj = {
r: colorArray[0],
g: colorArray[1],
b: colorArray[2],
a: colorArray[3]
};
return tinycolor(colorObj).toRgbString();
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
'Greys': [
[0, 'rgb(0,0,0)'], [1, 'rgb(255,255,255)']
],
'YlGnBu': [
[0, 'rgb(8,29,88)'], [0.125, 'rgb(37,52,148)'],
[0.25, 'rgb(34,94,168)'], [0.375, 'rgb(29,145,192)'],
[0.5, 'rgb(65,182,196)'], [0.625, 'rgb(127,205,187)'],
[0.75, 'rgb(199,233,180)'], [0.875, 'rgb(237,248,217)'],
[1, 'rgb(255,255,217)']
],
'Greens': [
[0, 'rgb(0,68,27)'], [0.125, 'rgb(0,109,44)'],
[0.25, 'rgb(35,139,69)'], [0.375, 'rgb(65,171,93)'],
[0.5, 'rgb(116,196,118)'], [0.625, 'rgb(161,217,155)'],
[0.75, 'rgb(199,233,192)'], [0.875, 'rgb(229,245,224)'],
[1, 'rgb(247,252,245)']
],
'YlOrRd': [
[0, 'rgb(128,0,38)'], [0.125, 'rgb(189,0,38)'],
[0.25, 'rgb(227,26,28)'], [0.375, 'rgb(252,78,42)'],
[0.5, 'rgb(253,141,60)'], [0.625, 'rgb(254,178,76)'],
[0.75, 'rgb(254,217,118)'], [0.875, 'rgb(255,237,160)'],
[1, 'rgb(255,255,204)']
],
'Bluered': [
[0, 'rgb(0,0,255)'], [1, 'rgb(255,0,0)']
],
// modified RdBu based on
// www.sandia.gov/~kmorel/documents/ColorMaps/ColorMapsExpanded.pdf
'RdBu': [
[0, 'rgb(5,10,172)'], [0.35, 'rgb(106,137,247)'],
[0.5, 'rgb(190,190,190)'], [0.6, 'rgb(220,170,132)'],
[0.7, 'rgb(230,145,90)'], [1, 'rgb(178,10,28)']
],
// Scale for non-negative numeric values
'Reds': [
[0, 'rgb(220,220,220)'], [0.2, 'rgb(245,195,157)'],
[0.4, 'rgb(245,160,105)'], [1, 'rgb(178,10,28)']
],
// Scale for non-positive numeric values
'Blues': [
[0, 'rgb(5,10,172)'], [0.35, 'rgb(40,60,190)'],
[0.5, 'rgb(70,100,245)'], [0.6, 'rgb(90,120,245)'],
[0.7, 'rgb(106,137,247)'], [1, 'rgb(220,220,220)']
],
'Picnic': [
[0, 'rgb(0,0,255)'], [0.1, 'rgb(51,153,255)'],
[0.2, 'rgb(102,204,255)'], [0.3, 'rgb(153,204,255)'],
[0.4, 'rgb(204,204,255)'], [0.5, 'rgb(255,255,255)'],
[0.6, 'rgb(255,204,255)'], [0.7, 'rgb(255,153,255)'],
[0.8, 'rgb(255,102,204)'], [0.9, 'rgb(255,102,102)'],
[1, 'rgb(255,0,0)']
],
'Rainbow': [
[0, 'rgb(150,0,90)'], [0.125, 'rgb(0,0,200)'],
[0.25, 'rgb(0,25,255)'], [0.375, 'rgb(0,152,255)'],
[0.5, 'rgb(44,255,150)'], [0.625, 'rgb(151,255,0)'],
[0.75, 'rgb(255,234,0)'], [0.875, 'rgb(255,111,0)'],
[1, 'rgb(255,0,0)']
],
'Portland': [
[0, 'rgb(12,51,131)'], [0.25, 'rgb(10,136,186)'],
[0.5, 'rgb(242,211,56)'], [0.75, 'rgb(242,143,56)'],
[1, 'rgb(217,30,30)']
],
'Jet': [
[0, 'rgb(0,0,131)'], [0.125, 'rgb(0,60,170)'],
[0.375, 'rgb(5,255,255)'], [0.625, 'rgb(255,255,0)'],
[0.875, 'rgb(250,0,0)'], [1, 'rgb(128,0,0)']
],
'Hot': [
[0, 'rgb(0,0,0)'], [0.3, 'rgb(230,0,0)'],
[0.6, 'rgb(255,210,0)'], [1, 'rgb(255,255,255)']
],
'Blackbody': [
[0, 'rgb(0,0,0)'], [0.2, 'rgb(230,0,0)'],
[0.4, 'rgb(230,210,0)'], [0.7, 'rgb(255,255,255)'],
[1, 'rgb(160,200,255)']
],
'Earth': [
[0, 'rgb(0,0,130)'], [0.1, 'rgb(0,180,180)'],
[0.2, 'rgb(40,210,40)'], [0.4, 'rgb(230,230,50)'],
[0.6, 'rgb(120,70,20)'], [1, 'rgb(255,255,255)']
],
'Electric': [
[0, 'rgb(0,0,0)'], [0.15, 'rgb(30,0,100)'],
[0.4, 'rgb(120,0,100)'], [0.6, 'rgb(160,90,0)'],
[0.8, 'rgb(230,200,0)'], [1, 'rgb(255,250,220)']
],
'Viridis': [
[0, '#440154'], [0.06274509803921569, '#48186a'],
[0.12549019607843137, '#472d7b'], [0.18823529411764706, '#424086'],
[0.25098039215686274, '#3b528b'], [0.3137254901960784, '#33638d'],
[0.3764705882352941, '#2c728e'], [0.4392156862745098, '#26828e'],
[0.5019607843137255, '#21918c'], [0.5647058823529412, '#1fa088'],
[0.6274509803921569, '#28ae80'], [0.6901960784313725, '#3fbc73'],
[0.7529411764705882, '#5ec962'], [0.8156862745098039, '#84d44b'],
[0.8784313725490196, '#addc30'], [0.9411764705882353, '#d8e219'],
[1, '#fde725']
]
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| align.js | 7.69% | (1 / 13) | 0% | (0 / 16) | 0% | (0 / 1) | 12.5% | (1 / 8) | |
| cursor.js | 16.67% | (3 / 18) | 0% | (0 / 12) | 0% | (0 / 1) | 25% | (3 / 12) | |
| index.js | 17.89% | (17 / 95) | 0% | (0 / 34) | 0% | (0 / 6) | 19.77% | (17 / 86) | |
| unhover.js | 18.18% | (4 / 22) | 0% | (0 / 14) | 0% | (0 / 2) | 20% | (4 / 20) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // for automatic alignment on dragging, <1/3 means left align, // >2/3 means right, and between is center. Pick the right fraction // based on where you are, and return the fraction corresponding to // that position on the object module.exports = function align(v, dv, v0, v1, anchor) { var vmin = (v - v0) / (v1 - v0), vmax = vmin + dv / (v1 - v0), vc = (vmin + vmax) / 2; // explicitly specified anchor if(anchor === 'left' || anchor === 'bottom') return vmin; if(anchor === 'center' || anchor === 'middle') return vc; if(anchor === 'right' || anchor === 'top') return vmax; // automatic based on position if(vmin < (2 / 3) - vc) return vmin; if(vmax > (4 / 3) - vc) return vmax; return vc; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
// set cursors pointing toward the closest corner/side,
// to indicate alignment
// x and y are 0-1, fractions of the plot area
var cursorset = [
['sw-resize', 's-resize', 'se-resize'],
['w-resize', 'move', 'e-resize'],
['nw-resize', 'n-resize', 'ne-resize']
];
module.exports = function getCursor(x, y, xanchor, yanchor) {
if(xanchor === 'left') x = 0;
else if(xanchor === 'center') x = 1;
else if(xanchor === 'right') x = 2;
else x = Lib.constrain(Math.floor(x * 3), 0, 2);
if(yanchor === 'bottom') y = 0;
else if(yanchor === 'middle') y = 1;
else if(yanchor === 'top') y = 2;
else y = Lib.constrain(Math.floor(y * 3), 0, 2);
return cursorset[y][x];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../../plotly');
var Lib = require('../../lib');
var constants = require('../../plots/cartesian/constants');
var interactConstants = require('../../constants/interactions');
var dragElement = module.exports = {};
dragElement.align = require('./align');
dragElement.getCursor = require('./cursor');
var unhover = require('./unhover');
dragElement.unhover = unhover.wrapped;
dragElement.unhoverRaw = unhover.raw;
/**
* Abstracts click & drag interactions
* @param {object} options with keys:
* element (required) the DOM element to drag
* prepFn (optional) function(event, startX, startY)
* executed on mousedown
* startX and startY are the clientX and clientY pixel position
* of the mousedown event
* moveFn (optional) function(dx, dy, dragged)
* executed on move
* dx and dy are the net pixel offset of the drag,
* dragged is true/false, has the mouse moved enough to
* constitute a drag
* doneFn (optional) function(dragged, numClicks, e)
* executed on mouseup, or mouseout of window since
* we don't get events after that
* dragged is as in moveFn
* numClicks is how many clicks we've registered within
* a doubleclick time
* e is the original event
* setCursor (optional) function(event)
* executed on mousemove before mousedown
* the purpose of this callback is to update the mouse cursor before
* the click & drag interaction has been initiated
*/
dragElement.init = function init(options) {
var gd = Lib.getPlotDiv(options.element) || {},
numClicks = 1,
DBLCLICKDELAY = interactConstants.DBLCLICKDELAY,
startX,
startY,
newMouseDownTime,
dragCover,
initialTarget,
initialOnMouseMove;
if(!gd._mouseDownTime) gd._mouseDownTime = 0;
function onStart(e) {
// disable call to options.setCursor(evt)
options.element.onmousemove = initialOnMouseMove;
// make dragging and dragged into properties of gd
// so that others can look at and modify them
gd._dragged = false;
gd._dragging = true;
startX = e.clientX;
startY = e.clientY;
initialTarget = e.target;
newMouseDownTime = (new Date()).getTime();
if(newMouseDownTime - gd._mouseDownTime < DBLCLICKDELAY) {
// in a click train
numClicks += 1;
}
else {
// new click train
numClicks = 1;
gd._mouseDownTime = newMouseDownTime;
}
if(options.prepFn) options.prepFn(e, startX, startY);
dragCover = coverSlip();
dragCover.onmousemove = onMove;
dragCover.onmouseup = onDone;
dragCover.onmouseout = onDone;
dragCover.style.cursor = window.getComputedStyle(options.element).cursor;
return Lib.pauseEvent(e);
}
function onMove(e) {
var dx = e.clientX - startX,
dy = e.clientY - startY,
minDrag = options.minDrag || constants.MINDRAG;
if(Math.abs(dx) < minDrag) dx = 0;
if(Math.abs(dy) < minDrag) dy = 0;
if(dx || dy) {
gd._dragged = true;
dragElement.unhover(gd);
}
if(options.moveFn) options.moveFn(dx, dy, gd._dragged);
return Lib.pauseEvent(e);
}
function onDone(e) {
// re-enable call to options.setCursor(evt)
initialOnMouseMove = options.element.onmousemove;
if(options.setCursor) options.element.onmousemove = options.setCursor;
dragCover.onmousemove = null;
dragCover.onmouseup = null;
dragCover.onmouseout = null;
Lib.removeElement(dragCover);
if(!gd._dragging) {
gd._dragged = false;
return;
}
gd._dragging = false;
// don't count as a dblClick unless the mouseUp is also within
// the dblclick delay
if((new Date()).getTime() - gd._mouseDownTime > DBLCLICKDELAY) {
numClicks = Math.max(numClicks - 1, 1);
}
if(options.doneFn) options.doneFn(gd._dragged, numClicks, e);
if(!gd._dragged) {
var e2;
try {
e2 = new MouseEvent('click', e);
}
catch(err) {
e2 = document.createEvent('MouseEvents');
e2.initMouseEvent('click',
e.bubbles, e.cancelable,
e.view, e.detail,
e.screenX, e.screenY,
e.clientX, e.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
e.button, e.relatedTarget);
}
initialTarget.dispatchEvent(e2);
}
finishDrag(gd);
gd._dragged = false;
return Lib.pauseEvent(e);
}
// enable call to options.setCursor(evt)
initialOnMouseMove = options.element.onmousemove;
if(options.setCursor) options.element.onmousemove = options.setCursor;
options.element.onmousedown = onStart;
options.element.style.pointerEvents = 'all';
};
function coverSlip() {
var cover = document.createElement('div');
cover.className = 'dragcover';
var cStyle = cover.style;
cStyle.position = 'fixed';
cStyle.left = 0;
cStyle.right = 0;
cStyle.top = 0;
cStyle.bottom = 0;
cStyle.zIndex = 999999999;
cStyle.background = 'none';
document.body.appendChild(cover);
return cover;
}
dragElement.coverSlip = coverSlip;
function finishDrag(gd) {
gd._dragging = false;
if(gd._replotPending) Plotly.plot(gd);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Events = require('../../lib/events');
var unhover = module.exports = {};
unhover.wrapped = function(gd, evt, subplot) {
if(typeof gd === 'string') gd = document.getElementById(gd);
// Important, clear any queued hovers
if(gd._hoverTimer) {
clearTimeout(gd._hoverTimer);
gd._hoverTimer = undefined;
}
unhover.raw(gd, evt, subplot);
};
// remove hover effects on mouse out, and emit unhover event
unhover.raw = function unhoverRaw(gd, evt) {
var fullLayout = gd._fullLayout;
var oldhoverdata = gd._hoverdata;
if(!evt) evt = {};
if(evt.target &&
Events.triggerHandler(gd, 'plotly_beforehover', evt) === false) {
return;
}
fullLayout._hoverlayer.selectAll('g').remove();
fullLayout._hoverlayer.selectAll('line').remove();
fullLayout._hoverlayer.selectAll('circle').remove();
gd._hoverdata = undefined;
if(evt.target && oldhoverdata) {
gd.emit('plotly_unhover', {
event: evt,
points: oldhoverdata
});
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| index.js | 21.45% | (65 / 303) | 1.6% | (4 / 250) | 2.04% | (1 / 49) | 23.13% | (65 / 281) | |
| symbol_defs.js | 2.15% | (2 / 93) | 100% | (0 / 0) | 0% | (0 / 45) | 2.15% | (2 / 93) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
exports.dash = {
valType: 'string',
// string type usually doesn't take values... this one should really be
// a special type or at least a special coercion function, from the GUI
// you only get these values but elsewhere the user can supply a list of
// dash lengths in px, and it will be honored
values: ['solid', 'dot', 'dash', 'longdash', 'dashdot', 'longdashdot'],
dflt: 'solid',
role: 'style',
description: [
'Sets the dash style of lines. Set to a dash type string',
'(*solid*, *dot*, *dash*, *longdash*, *dashdot*, or *longdashdot*)',
'or a dash length list in px (eg *5px,10px,2px,2px*).'
].join(' ')
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 45 45 45 45 45 18 45 19 26 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Color = require('../color');
var Colorscale = require('../colorscale');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var subTypes = require('../../traces/scatter/subtypes');
var makeBubbleSizeFn = require('../../traces/scatter/make_bubble_size_func');
var drawing = module.exports = {};
// -----------------------------------------------------
// styling functions for plot elements
// -----------------------------------------------------
drawing.font = function(s, family, size, color) {
// also allow the form font(s, {family, size, color})
if(family && family.family) {
color = family.color;
size = family.size;
family = family.family;
}
if(family) s.style('font-family', family);
if(size + 1) s.style('font-size', size + 'px');
if(color) s.call(Color.fill, color);
};
drawing.setPosition = function(s, x, y) { s.attr('x', x).attr('y', y); };
drawing.setSize = function(s, w, h) { s.attr('width', w).attr('height', h); };
drawing.setRect = function(s, x, y, w, h) {
s.call(drawing.setPosition, x, y).call(drawing.setSize, w, h);
};
drawing.translatePoint = function(d, sel, xa, ya) {
// put xp and yp into d if pixel scaling is already done
var x = d.xp || xa.c2p(d.x),
y = d.yp || ya.c2p(d.y);
if(isNumeric(x) && isNumeric(y) && sel.node()) {
// for multiline text this works better
if(sel.node().nodeName === 'text') {
sel.attr('x', x).attr('y', y);
} else {
sel.attr('transform', 'translate(' + x + ',' + y + ')');
}
}
else sel.remove();
};
drawing.translatePoints = function(s, xa, ya, trace) {
s.each(function(d) {
var sel = d3.select(this);
drawing.translatePoint(d, sel, xa, ya, trace);
});
};
drawing.getPx = function(s, styleAttr) {
// helper to pull out a px value from a style that may contain px units
// s is a d3 selection (will pull from the first one)
return Number(s.style(styleAttr).replace(/px$/, ''));
};
drawing.crispRound = function(gd, lineWidth, dflt) {
// for lines that disable antialiasing we want to
// make sure the width is an integer, and at least 1 if it's nonzero
if(!lineWidth || !isNumeric(lineWidth)) return dflt || 0;
// but not for static plots - these don't get antialiased anyway.
if(gd._context.staticPlot) return lineWidth;
if(lineWidth < 1) return 1;
return Math.round(lineWidth);
};
drawing.singleLineStyle = function(d, s, lw, lc, ld) {
s.style('fill', 'none');
var line = (((d || [])[0] || {}).trace || {}).line || {},
lw1 = lw || line.width||0,
dash = ld || line.dash || '';
Color.stroke(s, lc || line.color);
drawing.dashLine(s, dash, lw1);
};
drawing.lineGroupStyle = function(s, lw, lc, ld) {
s.style('fill', 'none')
.each(function(d) {
var line = (((d || [])[0] || {}).trace || {}).line || {},
lw1 = lw || line.width||0,
dash = ld || line.dash || '';
d3.select(this)
.call(Color.stroke, lc || line.color)
.call(drawing.dashLine, dash, lw1);
});
};
drawing.dashLine = function(s, dash, lineWidth) {
lineWidth = +lineWidth || 0;
dash = drawing.dashStyle(dash, lineWidth);
s.style({
'stroke-dasharray': dash,
'stroke-width': lineWidth + 'px'
});
};
drawing.dashStyle = function(dash, lineWidth) {
lineWidth = +lineWidth || 1;
var dlw = Math.max(lineWidth, 3);
if(dash === 'solid') dash = '';
else if(dash === 'dot') dash = dlw + 'px,' + dlw + 'px';
else if(dash === 'dash') dash = (3 * dlw) + 'px,' + (3 * dlw) + 'px';
else if(dash === 'longdash') dash = (5 * dlw) + 'px,' + (5 * dlw) + 'px';
else if(dash === 'dashdot') {
dash = (3 * dlw) + 'px,' + dlw + 'px,' + dlw + 'px,' + dlw + 'px';
}
else if(dash === 'longdashdot') {
dash = (5 * dlw) + 'px,' + (2 * dlw) + 'px,' + dlw + 'px,' + (2 * dlw) + 'px';
}
// otherwise user wrote the dasharray themselves - leave it be
return dash;
};
drawing.fillGroupStyle = function(s) {
s.style('stroke-width', 0)
.each(function(d) {
var shape = d3.select(this);
try {
shape.call(Color.fill, d[0].trace.fillcolor);
}
catch(e) {
Lib.error(e, s);
shape.remove();
}
});
};
var SYMBOLDEFS = require('./symbol_defs');
drawing.symbolNames = [];
drawing.symbolFuncs = [];
drawing.symbolNeedLines = {};
drawing.symbolNoDot = {};
drawing.symbolList = [];
Object.keys(SYMBOLDEFS).forEach(function(k) {
var symDef = SYMBOLDEFS[k];
drawing.symbolList = drawing.symbolList.concat(
[symDef.n, k, symDef.n + 100, k + '-open']);
drawing.symbolNames[symDef.n] = k;
drawing.symbolFuncs[symDef.n] = symDef.f;
if(symDef.needLine) {
drawing.symbolNeedLines[symDef.n] = true;
}
if(symDef.noDot) {
drawing.symbolNoDot[symDef.n] = true;
}
else {
drawing.symbolList = drawing.symbolList.concat(
[symDef.n + 200, k + '-dot', symDef.n + 300, k + '-open-dot']);
}
});
var MAXSYMBOL = drawing.symbolNames.length,
// add a dot in the middle of the symbol
DOTPATH = 'M0,0.5L0.5,0L0,-0.5L-0.5,0Z';
drawing.symbolNumber = function(v) {
if(typeof v === 'string') {
var vbase = 0;
if(v.indexOf('-open') > 0) {
vbase = 100;
v = v.replace('-open', '');
}
if(v.indexOf('-dot') > 0) {
vbase += 200;
v = v.replace('-dot', '');
}
v = drawing.symbolNames.indexOf(v);
if(v >= 0) { v += vbase; }
}
if((v % 100 >= MAXSYMBOL) || v >= 400) { return 0; }
return Math.floor(Math.max(v, 0));
};
function singlePointStyle(d, sel, trace, markerScale, lineScale, marker, markerLine) {
// only scatter & box plots get marker path and opacity
// bars, histograms don't
if(Registry.traceIs(trace, 'symbols')) {
var sizeFn = makeBubbleSizeFn(trace);
sel.attr('d', function(d) {
var r;
// handle multi-trace graph edit case
if(d.ms === 'various' || marker.size === 'various') r = 3;
else {
r = subTypes.isBubble(trace) ?
sizeFn(d.ms) : (marker.size || 6) / 2;
}
// store the calculated size so hover can use it
d.mrc = r;
// turn the symbol into a sanitized number
var x = drawing.symbolNumber(d.mx || marker.symbol) || 0,
xBase = x % 100;
// save if this marker is open
// because that impacts how to handle colors
d.om = x % 200 >= 100;
return drawing.symbolFuncs[xBase](r) +
(x >= 200 ? DOTPATH : '');
})
.style('opacity', function(d) {
return (d.mo + 1 || marker.opacity + 1) - 1;
});
}
// 'so' is suspected outliers, for box plots
var fillColor,
lineColor,
lineWidth;
if(d.so) {
lineWidth = markerLine.outlierwidth;
lineColor = markerLine.outliercolor;
fillColor = marker.outliercolor;
}
else {
lineWidth = (d.mlw + 1 || markerLine.width + 1 ||
// TODO: we need the latter for legends... can we get rid of it?
(d.trace ? d.trace.marker.line.width : 0) + 1) - 1;
if('mlc' in d) lineColor = d.mlcc = lineScale(d.mlc);
// weird case: array wasn't long enough to apply to every point
else if(Array.isArray(markerLine.color)) lineColor = Color.defaultLine;
else lineColor = markerLine.color;
if('mc' in d) fillColor = d.mcc = markerScale(d.mc);
else if(Array.isArray(marker.color)) fillColor = Color.defaultLine;
else fillColor = marker.color || 'rgba(0,0,0,0)';
}
if(d.om) {
// open markers can't have zero linewidth, default to 1px,
// and use fill color as stroke color
sel.call(Color.stroke, fillColor)
.style({
'stroke-width': (lineWidth || 1) + 'px',
fill: 'none'
});
}
else {
sel.style('stroke-width', lineWidth + 'px')
.call(Color.fill, fillColor);
if(lineWidth) {
sel.call(Color.stroke, lineColor);
}
}
}
drawing.singlePointStyle = function(d, sel, trace) {
var marker = trace.marker,
markerLine = marker.line;
// allow array marker and marker line colors to be
// scaled by given max and min to colorscales
var markerScale = drawing.tryColorscale(marker, ''),
lineScale = drawing.tryColorscale(marker, 'line');
singlePointStyle(d, sel, trace, markerScale, lineScale, marker, markerLine);
};
drawing.pointStyle = function(s, trace) {
if(!s.size()) return;
// allow array marker and marker line colors to be
// scaled by given max and min to colorscales
var marker = trace.marker;
var markerScale = drawing.tryColorscale(marker, ''),
lineScale = drawing.tryColorscale(marker, 'line');
s.each(function(d) {
drawing.singlePointStyle(d, d3.select(this), trace, markerScale, lineScale);
});
};
drawing.tryColorscale = function(marker, prefix) {
var cont = prefix ? Lib.nestedProperty(marker, prefix).get() : marker,
scl = cont.colorscale,
colorArray = cont.color;
if(scl && Array.isArray(colorArray)) {
return Colorscale.makeColorScaleFunc(
Colorscale.extractScale(scl, cont.cmin, cont.cmax)
);
}
else return Lib.identity;
};
// draw text at points
var TEXTOFFSETSIGN = {start: 1, end: -1, middle: 0, bottom: 1, top: -1},
LINEEXPAND = 1.3;
drawing.textPointStyle = function(s, trace) {
s.each(function(d) {
var p = d3.select(this),
text = d.tx || trace.text;
if(!text || Array.isArray(text)) {
// isArray test handles the case of (intentionally) missing
// or empty text within a text array
p.remove();
return;
}
var pos = d.tp || trace.textposition,
v = pos.indexOf('top') !== -1 ? 'top' :
pos.indexOf('bottom') !== -1 ? 'bottom' : 'middle',
h = pos.indexOf('left') !== -1 ? 'end' :
pos.indexOf('right') !== -1 ? 'start' : 'middle',
fontSize = d.ts || trace.textfont.size,
// if markers are shown, offset a little more than
// the nominal marker size
// ie 2/1.6 * nominal, bcs some markers are a bit bigger
r = d.mrc ? (d.mrc / 0.8 + 1) : 0;
fontSize = (isNumeric(fontSize) && fontSize > 0) ? fontSize : 0;
p.call(drawing.font,
d.tf || trace.textfont.family,
fontSize,
d.tc || trace.textfont.color)
.attr('text-anchor', h)
.text(text)
.call(svgTextUtils.convertToTspans);
var pgroup = d3.select(this.parentNode),
tspans = p.selectAll('tspan.line'),
numLines = ((tspans[0].length || 1) - 1) * LINEEXPAND + 1,
dx = TEXTOFFSETSIGN[h] * r,
dy = fontSize * 0.75 + TEXTOFFSETSIGN[v] * r +
(TEXTOFFSETSIGN[v] - 1) * numLines * fontSize / 2;
// fix the overall text group position
pgroup.attr('transform', 'translate(' + dx + ',' + dy + ')');
// then fix multiline text
if(numLines > 1) {
tspans.attr({ x: p.attr('x'), y: p.attr('y') });
}
});
};
// generalized Catmull-Rom splines, per
// http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
var CatmullRomExp = 0.5;
drawing.smoothopen = function(pts, smoothness) {
if(pts.length < 3) { return 'M' + pts.join('L');}
var path = 'M' + pts[0],
tangents = [], i;
for(i = 1; i < pts.length - 1; i++) {
tangents.push(makeTangent(pts[i - 1], pts[i], pts[i + 1], smoothness));
}
path += 'Q' + tangents[0][0] + ' ' + pts[1];
for(i = 2; i < pts.length - 1; i++) {
path += 'C' + tangents[i - 2][1] + ' ' + tangents[i - 1][0] + ' ' + pts[i];
}
path += 'Q' + tangents[pts.length - 3][1] + ' ' + pts[pts.length - 1];
return path;
};
drawing.smoothclosed = function(pts, smoothness) {
if(pts.length < 3) { return 'M' + pts.join('L') + 'Z'; }
var path = 'M' + pts[0],
pLast = pts.length - 1,
tangents = [makeTangent(pts[pLast],
pts[0], pts[1], smoothness)],
i;
for(i = 1; i < pLast; i++) {
tangents.push(makeTangent(pts[i - 1], pts[i], pts[i + 1], smoothness));
}
tangents.push(
makeTangent(pts[pLast - 1], pts[pLast], pts[0], smoothness)
);
for(i = 1; i <= pLast; i++) {
path += 'C' + tangents[i - 1][1] + ' ' + tangents[i][0] + ' ' + pts[i];
}
path += 'C' + tangents[pLast][1] + ' ' + tangents[0][0] + ' ' + pts[0] + 'Z';
return path;
};
function makeTangent(prevpt, thispt, nextpt, smoothness) {
var d1x = prevpt[0] - thispt[0],
d1y = prevpt[1] - thispt[1],
d2x = nextpt[0] - thispt[0],
d2y = nextpt[1] - thispt[1],
d1a = Math.pow(d1x * d1x + d1y * d1y, CatmullRomExp / 2),
d2a = Math.pow(d2x * d2x + d2y * d2y, CatmullRomExp / 2),
numx = (d2a * d2a * d1x - d1a * d1a * d2x) * smoothness,
numy = (d2a * d2a * d1y - d1a * d1a * d2y) * smoothness,
denom1 = 3 * d2a * (d1a + d2a),
denom2 = 3 * d1a * (d1a + d2a);
return [
[
d3.round(thispt[0] + (denom1 && numx / denom1), 2),
d3.round(thispt[1] + (denom1 && numy / denom1), 2)
], [
d3.round(thispt[0] - (denom2 && numx / denom2), 2),
d3.round(thispt[1] - (denom2 && numy / denom2), 2)
]
];
}
// step paths - returns a generator function for paths
// with the given step shape
var STEPPATH = {
hv: function(p0, p1) {
return 'H' + d3.round(p1[0], 2) + 'V' + d3.round(p1[1], 2);
},
vh: function(p0, p1) {
return 'V' + d3.round(p1[1], 2) + 'H' + d3.round(p1[0], 2);
},
hvh: function(p0, p1) {
return 'H' + d3.round((p0[0] + p1[0]) / 2, 2) + 'V' +
d3.round(p1[1], 2) + 'H' + d3.round(p1[0], 2);
},
vhv: function(p0, p1) {
return 'V' + d3.round((p0[1] + p1[1]) / 2, 2) + 'H' +
d3.round(p1[0], 2) + 'V' + d3.round(p1[1], 2);
}
};
var STEPLINEAR = function(p0, p1) {
return 'L' + d3.round(p1[0], 2) + ',' + d3.round(p1[1], 2);
};
drawing.steps = function(shape) {
var onestep = STEPPATH[shape] || STEPLINEAR;
return function(pts) {
var path = 'M' + d3.round(pts[0][0], 2) + ',' + d3.round(pts[0][1], 2);
for(var i = 1; i < pts.length; i++) {
path += onestep(pts[i - 1], pts[i]);
}
return path;
};
};
// off-screen svg render testing element, shared by the whole page
// uses the id 'js-plotly-tester' and stores it in gd._tester
// makes a hash of cached text items in tester.node()._cache
// so we can add references to rendered text (including all info
// needed to fully determine its bounding rect)
drawing.makeTester = function(gd) {
var tester = d3.select('body')
.selectAll('#js-plotly-tester')
.data([0]);
tester.enter().append('svg')
.attr('id', 'js-plotly-tester')
.attr(xmlnsNamespaces.svgAttrs)
.style({
position: 'absolute',
left: '-10000px',
top: '-10000px',
width: '9000px',
height: '9000px',
'z-index': '1'
});
// browsers differ on how they describe the bounding rect of
// the svg if its contents spill over... so make a 1x1px
// reference point we can measure off of.
var testref = tester.selectAll('.js-reference-point').data([0]);
testref.enter().append('path')
.classed('js-reference-point', true)
.attr('d', 'M0,0H1V1H0Z')
.style({
'stroke-width': 0,
fill: 'black'
});
if(!tester.node()._cache) {
tester.node()._cache = {};
}
gd._tester = tester;
gd._testref = testref;
};
// use our offscreen tester to get a clientRect for an element,
// in a reference frame where it isn't translated and its anchor
// point is at (0,0)
// always returns a copy of the bbox, so the caller can modify it safely
var savedBBoxes = [],
maxSavedBBoxes = 10000;
drawing.bBox = function(node) {
// cache elements we've already measured so we don't have to
// remeasure the same thing many times
var saveNum = node.attributes['data-bb'];
if(saveNum && saveNum.value) {
return Lib.extendFlat({}, savedBBoxes[saveNum.value]);
}
var test3 = d3.select('#js-plotly-tester'),
tester = test3.node();
// copy the node to test into the tester
var testNode = node.cloneNode(true);
tester.appendChild(testNode);
// standardize its position... do we really want to do this?
d3.select(testNode).attr({
x: 0,
y: 0,
transform: ''
});
var testRect = testNode.getBoundingClientRect(),
refRect = test3.select('.js-reference-point')
.node().getBoundingClientRect();
tester.removeChild(testNode);
var bb = {
height: testRect.height,
width: testRect.width,
left: testRect.left - refRect.left,
top: testRect.top - refRect.top,
right: testRect.right - refRect.left,
bottom: testRect.bottom - refRect.top
};
// make sure we don't have too many saved boxes,
// or a long session could overload on memory
// by saving boxes for long-gone elements
if(savedBBoxes.length >= maxSavedBBoxes) {
d3.selectAll('[data-bb]').attr('data-bb', null);
savedBBoxes = [];
}
// cache this bbox
node.setAttribute('data-bb', savedBBoxes.length);
savedBBoxes.push(bb);
return Lib.extendFlat({}, bb);
};
/*
* make a robust clipPath url from a local id
* note! We'd better not be exporting from a page
* with a <base> or the svg will not be portable!
*/
drawing.setClipUrl = function(s, localId) {
if(!localId) {
s.attr('clip-path', null);
return;
}
var url = '#' + localId,
base = d3.select('base');
// add id to location href w/o hashes if any)
if(base.size() && base.attr('href')) {
url = window.location.href.split('#')[0] + url;
}
s.attr('clip-path', 'url(' + url + ')');
};
drawing.getTranslate = function(element) {
// Note the separator [^\d] between x and y in this regex
// We generally use ',' but IE will convert it to ' '
var re = /.*\btranslate\((-?\d*\.?\d*)[^-\d]*(-?\d*\.?\d*)[^\d].*/,
getter = element.attr ? 'attr' : 'getAttribute',
transform = element[getter]('transform') || '';
var translate = transform.replace(re, function(match, p1, p2) {
return [p1, p2].join(' ');
})
.split(' ');
return {
x: +translate[0] || 0,
y: +translate[1] || 0
};
};
drawing.setTranslate = function(element, x, y) {
var re = /(\btranslate\(.*?\);?)/,
getter = element.attr ? 'attr' : 'getAttribute',
setter = element.attr ? 'attr' : 'setAttribute',
transform = element[getter]('transform') || '';
x = x || 0;
y = y || 0;
transform = transform.replace(re, '').trim();
transform += ' translate(' + x + ', ' + y + ')';
transform = transform.trim();
element[setter]('transform', transform);
return transform;
};
drawing.getScale = function(element) {
var re = /.*\bscale\((\d*\.?\d*)[^\d]*(\d*\.?\d*)[^\d].*/,
getter = element.attr ? 'attr' : 'getAttribute',
transform = element[getter]('transform') || '';
var translate = transform.replace(re, function(match, p1, p2) {
return [p1, p2].join(' ');
})
.split(' ');
return {
x: +translate[0] || 1,
y: +translate[1] || 1
};
};
drawing.setScale = function(element, x, y) {
var re = /(\bscale\(.*?\);?)/,
getter = element.attr ? 'attr' : 'getAttribute',
setter = element.attr ? 'attr' : 'setAttribute',
transform = element[getter]('transform') || '';
x = x || 1;
y = y || 1;
transform = transform.replace(re, '').trim();
transform += ' scale(' + x + ', ' + y + ')';
transform = transform.trim();
element[setter]('transform', transform);
return transform;
};
drawing.setPointGroupScale = function(selection, x, y) {
var t, scale, re;
x = x || 1;
y = y || 1;
if(x === 1 && y === 1) {
scale = '';
} else {
// The same scale transform for every point:
scale = ' scale(' + x + ',' + y + ')';
}
// A regex to strip any existing scale:
re = /\s*sc.*/;
selection.each(function() {
// Get the transform:
t = (this.getAttribute('transform') || '').replace(re, '');
t += scale;
t = t.trim();
// Append the scale transform
this.setAttribute('transform', t);
});
return scale;
};
drawing.measureText = function(tester, text, font) {
var dummyText = tester.append('text')
.text(text)
.call(drawing.font, font);
var bbox = drawing.bBox(dummyText.node());
dummyText.remove();
return bbox;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
/** Marker symbol definitions
* users can specify markers either by number or name
* add 100 (or '-open') and you get an open marker
* open markers have no fill and use line color as the stroke color
* add 200 (or '-dot') and you get a dot in the middle
* add both and you get both
*/
module.exports = {
circle: {
n: 0,
f: function(r) {
var rs = d3.round(r, 2);
return 'M' + rs + ',0A' + rs + ',' + rs + ' 0 1,1 0,-' + rs +
'A' + rs + ',' + rs + ' 0 0,1 ' + rs + ',0Z';
}
},
square: {
n: 1,
f: function(r) {
var rs = d3.round(r, 2);
return 'M' + rs + ',' + rs + 'H-' + rs + 'V-' + rs + 'H' + rs + 'Z';
}
},
diamond: {
n: 2,
f: function(r) {
var rd = d3.round(r * 1.3, 2);
return 'M' + rd + ',0L0,' + rd + 'L-' + rd + ',0L0,-' + rd + 'Z';
}
},
cross: {
n: 3,
f: function(r) {
var rc = d3.round(r * 0.4, 2),
rc2 = d3.round(r * 1.2, 2);
return 'M' + rc2 + ',' + rc + 'H' + rc + 'V' + rc2 + 'H-' + rc +
'V' + rc + 'H-' + rc2 + 'V-' + rc + 'H-' + rc + 'V-' + rc2 +
'H' + rc + 'V-' + rc + 'H' + rc2 + 'Z';
}
},
x: {
n: 4,
f: function(r) {
var rx = d3.round(r * 0.8 / Math.sqrt(2), 2),
ne = 'l' + rx + ',' + rx,
se = 'l' + rx + ',-' + rx,
sw = 'l-' + rx + ',-' + rx,
nw = 'l-' + rx + ',' + rx;
return 'M0,' + rx + ne + se + sw + se + sw + nw + sw + nw + ne + nw + ne + 'Z';
}
},
'triangle-up': {
n: 5,
f: function(r) {
var rt = d3.round(r * 2 / Math.sqrt(3), 2),
r2 = d3.round(r / 2, 2),
rs = d3.round(r, 2);
return 'M-' + rt + ',' + r2 + 'H' + rt + 'L0,-' + rs + 'Z';
}
},
'triangle-down': {
n: 6,
f: function(r) {
var rt = d3.round(r * 2 / Math.sqrt(3), 2),
r2 = d3.round(r / 2, 2),
rs = d3.round(r, 2);
return 'M-' + rt + ',-' + r2 + 'H' + rt + 'L0,' + rs + 'Z';
}
},
'triangle-left': {
n: 7,
f: function(r) {
var rt = d3.round(r * 2 / Math.sqrt(3), 2),
r2 = d3.round(r / 2, 2),
rs = d3.round(r, 2);
return 'M' + r2 + ',-' + rt + 'V' + rt + 'L-' + rs + ',0Z';
}
},
'triangle-right': {
n: 8,
f: function(r) {
var rt = d3.round(r * 2 / Math.sqrt(3), 2),
r2 = d3.round(r / 2, 2),
rs = d3.round(r, 2);
return 'M-' + r2 + ',-' + rt + 'V' + rt + 'L' + rs + ',0Z';
}
},
'triangle-ne': {
n: 9,
f: function(r) {
var r1 = d3.round(r * 0.6, 2),
r2 = d3.round(r * 1.2, 2);
return 'M-' + r2 + ',-' + r1 + 'H' + r1 + 'V' + r2 + 'Z';
}
},
'triangle-se': {
n: 10,
f: function(r) {
var r1 = d3.round(r * 0.6, 2),
r2 = d3.round(r * 1.2, 2);
return 'M' + r1 + ',-' + r2 + 'V' + r1 + 'H-' + r2 + 'Z';
}
},
'triangle-sw': {
n: 11,
f: function(r) {
var r1 = d3.round(r * 0.6, 2),
r2 = d3.round(r * 1.2, 2);
return 'M' + r2 + ',' + r1 + 'H-' + r1 + 'V-' + r2 + 'Z';
}
},
'triangle-nw': {
n: 12,
f: function(r) {
var r1 = d3.round(r * 0.6, 2),
r2 = d3.round(r * 1.2, 2);
return 'M-' + r1 + ',' + r2 + 'V-' + r1 + 'H' + r2 + 'Z';
}
},
pentagon: {
n: 13,
f: function(r) {
var x1 = d3.round(r * 0.951, 2),
x2 = d3.round(r * 0.588, 2),
y0 = d3.round(-r, 2),
y1 = d3.round(r * -0.309, 2),
y2 = d3.round(r * 0.809, 2);
return 'M' + x1 + ',' + y1 + 'L' + x2 + ',' + y2 + 'H-' + x2 +
'L-' + x1 + ',' + y1 + 'L0,' + y0 + 'Z';
}
},
hexagon: {
n: 14,
f: function(r) {
var y0 = d3.round(r, 2),
y1 = d3.round(r / 2, 2),
x = d3.round(r * Math.sqrt(3) / 2, 2);
return 'M' + x + ',-' + y1 + 'V' + y1 + 'L0,' + y0 +
'L-' + x + ',' + y1 + 'V-' + y1 + 'L0,-' + y0 + 'Z';
}
},
hexagon2: {
n: 15,
f: function(r) {
var x0 = d3.round(r, 2),
x1 = d3.round(r / 2, 2),
y = d3.round(r * Math.sqrt(3) / 2, 2);
return 'M-' + x1 + ',' + y + 'H' + x1 + 'L' + x0 +
',0L' + x1 + ',-' + y + 'H-' + x1 + 'L-' + x0 + ',0Z';
}
},
octagon: {
n: 16,
f: function(r) {
var a = d3.round(r * 0.924, 2),
b = d3.round(r * 0.383, 2);
return 'M-' + b + ',-' + a + 'H' + b + 'L' + a + ',-' + b + 'V' + b +
'L' + b + ',' + a + 'H-' + b + 'L-' + a + ',' + b + 'V-' + b + 'Z';
}
},
star: {
n: 17,
f: function(r) {
var rs = r * 1.4,
x1 = d3.round(rs * 0.225, 2),
x2 = d3.round(rs * 0.951, 2),
x3 = d3.round(rs * 0.363, 2),
x4 = d3.round(rs * 0.588, 2),
y0 = d3.round(-rs, 2),
y1 = d3.round(rs * -0.309, 2),
y3 = d3.round(rs * 0.118, 2),
y4 = d3.round(rs * 0.809, 2),
y5 = d3.round(rs * 0.382, 2);
return 'M' + x1 + ',' + y1 + 'H' + x2 + 'L' + x3 + ',' + y3 +
'L' + x4 + ',' + y4 + 'L0,' + y5 + 'L-' + x4 + ',' + y4 +
'L-' + x3 + ',' + y3 + 'L-' + x2 + ',' + y1 + 'H-' + x1 +
'L0,' + y0 + 'Z';
}
},
hexagram: {
n: 18,
f: function(r) {
var y = d3.round(r * 0.66, 2),
x1 = d3.round(r * 0.38, 2),
x2 = d3.round(r * 0.76, 2);
return 'M-' + x2 + ',0l-' + x1 + ',-' + y + 'h' + x2 +
'l' + x1 + ',-' + y + 'l' + x1 + ',' + y + 'h' + x2 +
'l-' + x1 + ',' + y + 'l' + x1 + ',' + y + 'h-' + x2 +
'l-' + x1 + ',' + y + 'l-' + x1 + ',-' + y + 'h-' + x2 + 'Z';
}
},
'star-triangle-up': {
n: 19,
f: function(r) {
var x = d3.round(r * Math.sqrt(3) * 0.8, 2),
y1 = d3.round(r * 0.8, 2),
y2 = d3.round(r * 1.6, 2),
rc = d3.round(r * 4, 2),
aPart = 'A ' + rc + ',' + rc + ' 0 0 1 ';
return 'M-' + x + ',' + y1 + aPart + x + ',' + y1 +
aPart + '0,-' + y2 + aPart + '-' + x + ',' + y1 + 'Z';
}
},
'star-triangle-down': {
n: 20,
f: function(r) {
var x = d3.round(r * Math.sqrt(3) * 0.8, 2),
y1 = d3.round(r * 0.8, 2),
y2 = d3.round(r * 1.6, 2),
rc = d3.round(r * 4, 2),
aPart = 'A ' + rc + ',' + rc + ' 0 0 1 ';
return 'M' + x + ',-' + y1 + aPart + '-' + x + ',-' + y1 +
aPart + '0,' + y2 + aPart + x + ',-' + y1 + 'Z';
}
},
'star-square': {
n: 21,
f: function(r) {
var rp = d3.round(r * 1.1, 2),
rc = d3.round(r * 2, 2),
aPart = 'A ' + rc + ',' + rc + ' 0 0 1 ';
return 'M-' + rp + ',-' + rp + aPart + '-' + rp + ',' + rp +
aPart + rp + ',' + rp + aPart + rp + ',-' + rp +
aPart + '-' + rp + ',-' + rp + 'Z';
}
},
'star-diamond': {
n: 22,
f: function(r) {
var rp = d3.round(r * 1.4, 2),
rc = d3.round(r * 1.9, 2),
aPart = 'A ' + rc + ',' + rc + ' 0 0 1 ';
return 'M-' + rp + ',0' + aPart + '0,' + rp +
aPart + rp + ',0' + aPart + '0,-' + rp +
aPart + '-' + rp + ',0' + 'Z';
}
},
'diamond-tall': {
n: 23,
f: function(r) {
var x = d3.round(r * 0.7, 2),
y = d3.round(r * 1.4, 2);
return 'M0,' + y + 'L' + x + ',0L0,-' + y + 'L-' + x + ',0Z';
}
},
'diamond-wide': {
n: 24,
f: function(r) {
var x = d3.round(r * 1.4, 2),
y = d3.round(r * 0.7, 2);
return 'M0,' + y + 'L' + x + ',0L0,-' + y + 'L-' + x + ',0Z';
}
},
hourglass: {
n: 25,
f: function(r) {
var rs = d3.round(r, 2);
return 'M' + rs + ',' + rs + 'H-' + rs + 'L' + rs + ',-' + rs + 'H-' + rs + 'Z';
},
noDot: true
},
bowtie: {
n: 26,
f: function(r) {
var rs = d3.round(r, 2);
return 'M' + rs + ',' + rs + 'V-' + rs + 'L-' + rs + ',' + rs + 'V-' + rs + 'Z';
},
noDot: true
},
'circle-cross': {
n: 27,
f: function(r) {
var rs = d3.round(r, 2);
return 'M0,' + rs + 'V-' + rs + 'M' + rs + ',0H-' + rs +
'M' + rs + ',0A' + rs + ',' + rs + ' 0 1,1 0,-' + rs +
'A' + rs + ',' + rs + ' 0 0,1 ' + rs + ',0Z';
},
needLine: true,
noDot: true
},
'circle-x': {
n: 28,
f: function(r) {
var rs = d3.round(r, 2),
rc = d3.round(r / Math.sqrt(2), 2);
return 'M' + rc + ',' + rc + 'L-' + rc + ',-' + rc +
'M' + rc + ',-' + rc + 'L-' + rc + ',' + rc +
'M' + rs + ',0A' + rs + ',' + rs + ' 0 1,1 0,-' + rs +
'A' + rs + ',' + rs + ' 0 0,1 ' + rs + ',0Z';
},
needLine: true,
noDot: true
},
'square-cross': {
n: 29,
f: function(r) {
var rs = d3.round(r, 2);
return 'M0,' + rs + 'V-' + rs + 'M' + rs + ',0H-' + rs +
'M' + rs + ',' + rs + 'H-' + rs + 'V-' + rs + 'H' + rs + 'Z';
},
needLine: true,
noDot: true
},
'square-x': {
n: 30,
f: function(r) {
var rs = d3.round(r, 2);
return 'M' + rs + ',' + rs + 'L-' + rs + ',-' + rs +
'M' + rs + ',-' + rs + 'L-' + rs + ',' + rs +
'M' + rs + ',' + rs + 'H-' + rs + 'V-' + rs + 'H' + rs + 'Z';
},
needLine: true,
noDot: true
},
'diamond-cross': {
n: 31,
f: function(r) {
var rd = d3.round(r * 1.3, 2);
return 'M' + rd + ',0L0,' + rd + 'L-' + rd + ',0L0,-' + rd + 'Z' +
'M0,-' + rd + 'V' + rd + 'M-' + rd + ',0H' + rd;
},
needLine: true,
noDot: true
},
'diamond-x': {
n: 32,
f: function(r) {
var rd = d3.round(r * 1.3, 2),
r2 = d3.round(r * 0.65, 2);
return 'M' + rd + ',0L0,' + rd + 'L-' + rd + ',0L0,-' + rd + 'Z' +
'M-' + r2 + ',-' + r2 + 'L' + r2 + ',' + r2 +
'M-' + r2 + ',' + r2 + 'L' + r2 + ',-' + r2;
},
needLine: true,
noDot: true
},
'cross-thin': {
n: 33,
f: function(r) {
var rc = d3.round(r * 1.4, 2);
return 'M0,' + rc + 'V-' + rc + 'M' + rc + ',0H-' + rc;
},
needLine: true,
noDot: true
},
'x-thin': {
n: 34,
f: function(r) {
var rx = d3.round(r, 2);
return 'M' + rx + ',' + rx + 'L-' + rx + ',-' + rx +
'M' + rx + ',-' + rx + 'L-' + rx + ',' + rx;
},
needLine: true,
noDot: true
},
asterisk: {
n: 35,
f: function(r) {
var rc = d3.round(r * 1.2, 2);
var rs = d3.round(r * 0.85, 2);
return 'M0,' + rc + 'V-' + rc + 'M' + rc + ',0H-' + rc +
'M' + rs + ',' + rs + 'L-' + rs + ',-' + rs +
'M' + rs + ',-' + rs + 'L-' + rs + ',' + rs;
},
needLine: true,
noDot: true
},
hash: {
n: 36,
f: function(r) {
var r1 = d3.round(r / 2, 2),
r2 = d3.round(r, 2);
return 'M' + r1 + ',' + r2 + 'V-' + r2 +
'm-' + r2 + ',0V' + r2 +
'M' + r2 + ',' + r1 + 'H-' + r2 +
'm0,-' + r2 + 'H' + r2;
},
needLine: true
},
'y-up': {
n: 37,
f: function(r) {
var x = d3.round(r * 1.2, 2),
y0 = d3.round(r * 1.6, 2),
y1 = d3.round(r * 0.8, 2);
return 'M-' + x + ',' + y1 + 'L0,0M' + x + ',' + y1 + 'L0,0M0,-' + y0 + 'L0,0';
},
needLine: true,
noDot: true
},
'y-down': {
n: 38,
f: function(r) {
var x = d3.round(r * 1.2, 2),
y0 = d3.round(r * 1.6, 2),
y1 = d3.round(r * 0.8, 2);
return 'M-' + x + ',-' + y1 + 'L0,0M' + x + ',-' + y1 + 'L0,0M0,' + y0 + 'L0,0';
},
needLine: true,
noDot: true
},
'y-left': {
n: 39,
f: function(r) {
var y = d3.round(r * 1.2, 2),
x0 = d3.round(r * 1.6, 2),
x1 = d3.round(r * 0.8, 2);
return 'M' + x1 + ',' + y + 'L0,0M' + x1 + ',-' + y + 'L0,0M-' + x0 + ',0L0,0';
},
needLine: true,
noDot: true
},
'y-right': {
n: 40,
f: function(r) {
var y = d3.round(r * 1.2, 2),
x0 = d3.round(r * 1.6, 2),
x1 = d3.round(r * 0.8, 2);
return 'M-' + x1 + ',' + y + 'L0,0M-' + x1 + ',-' + y + 'L0,0M' + x0 + ',0L0,0';
},
needLine: true,
noDot: true
},
'line-ew': {
n: 41,
f: function(r) {
var rc = d3.round(r * 1.4, 2);
return 'M' + rc + ',0H-' + rc;
},
needLine: true,
noDot: true
},
'line-ns': {
n: 42,
f: function(r) {
var rc = d3.round(r * 1.4, 2);
return 'M0,' + rc + 'V-' + rc;
},
needLine: true,
noDot: true
},
'line-ne': {
n: 43,
f: function(r) {
var rx = d3.round(r, 2);
return 'M' + rx + ',-' + rx + 'L-' + rx + ',' + rx;
},
needLine: true,
noDot: true
},
'line-nw': {
n: 44,
f: function(r) {
var rx = d3.round(r, 2);
return 'M' + rx + ',' + rx + 'L-' + rx + ',-' + rx;
},
needLine: true,
noDot: true
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| calc.js | 22.22% | (6 / 27) | 0% | (0 / 14) | 0% | (0 / 2) | 25% | (6 / 24) | |
| compute_error.js | 7.41% | (2 / 27) | 0% | (0 / 16) | 0% | (0 / 9) | 7.41% | (2 / 27) | |
| defaults.js | 15.79% | (6 / 38) | 0% | (0 / 42) | 0% | (0 / 2) | 17.65% | (6 / 34) | |
| index.js | 34.78% | (8 / 23) | 0% | (0 / 18) | 0% | (0 / 2) | 38.1% | (8 / 21) | |
| plot.js | 7.46% | (5 / 67) | 0% | (0 / 54) | 0% | (0 / 5) | 8.06% | (5 / 62) | |
| style.js | 30% | (3 / 10) | 0% | (0 / 6) | 0% | (0 / 2) | 33.33% | (3 / 9) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
visible: {
valType: 'boolean',
role: 'info',
description: [
'Determines whether or not this set of error bars is visible.'
].join(' ')
},
type: {
valType: 'enumerated',
values: ['percent', 'constant', 'sqrt', 'data'],
role: 'info',
description: [
'Determines the rule used to generate the error bars.',
'If *constant`, the bar lengths are of a constant value.',
'Set this constant in `value`.',
'If *percent*, the bar lengths correspond to a percentage of',
'underlying data. Set this percentage in `value`.',
'If *sqrt*, the bar lengths correspond to the sqaure of the',
'underlying data.',
'If *array*, the bar lengths are set with data set `array`.'
].join(' ')
},
symmetric: {
valType: 'boolean',
role: 'info',
description: [
'Determines whether or not the error bars have the same length',
'in both direction',
'(top/bottom for vertical bars, left/right for horizontal bars.'
].join(' ')
},
array: {
valType: 'data_array',
description: [
'Sets the data corresponding the length of each error bar.',
'Values are plotted relative to the underlying data.'
].join(' ')
},
arrayminus: {
valType: 'data_array',
description: [
'Sets the data corresponding the length of each error bar in the',
'bottom (left) direction for vertical (horizontal) bars',
'Values are plotted relative to the underlying data.'
].join(' ')
},
value: {
valType: 'number',
min: 0,
dflt: 10,
role: 'info',
description: [
'Sets the value of either the percentage',
'(if `type` is set to *percent*) or the constant',
'(if `type` is set to *constant*) corresponding to the lengths of',
'the error bars.'
].join(' ')
},
valueminus: {
valType: 'number',
min: 0,
dflt: 10,
role: 'info',
description: [
'Sets the value of either the percentage',
'(if `type` is set to *percent*) or the constant',
'(if `type` is set to *constant*) corresponding to the lengths of',
'the error bars in the',
'bottom (left) direction for vertical (horizontal) bars'
].join(' ')
},
traceref: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'info'
},
tracerefminus: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'info'
},
copy_ystyle: {
valType: 'boolean',
role: 'style'
},
copy_zstyle: {
valType: 'boolean',
role: 'style'
},
color: {
valType: 'color',
role: 'style',
description: 'Sets the stoke color of the error bars.'
},
thickness: {
valType: 'number',
min: 0,
dflt: 2,
role: 'style',
description: 'Sets the thickness (in px) of the error bars.'
},
width: {
valType: 'number',
min: 0,
role: 'style',
description: [
'Sets the width (in px) of the cross-bar at both ends',
'of the error bars.'
].join(' ')
},
_deprecated: {
opacity: {
valType: 'number',
role: 'style',
description: [
'Obsolete.',
'Use the alpha channel in error bar `color` to set the opacity.'
].join(' ')
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var makeComputeError = require('./compute_error');
module.exports = function calc(gd) {
var calcdata = gd.calcdata;
for(var i = 0; i < calcdata.length; i++) {
var calcTrace = calcdata[i],
trace = calcTrace[0].trace;
if(!Registry.traceIs(trace, 'errorBarsOK')) continue;
var xa = Axes.getFromId(gd, trace.xaxis),
ya = Axes.getFromId(gd, trace.yaxis);
calcOneAxis(calcTrace, trace, xa, 'x');
calcOneAxis(calcTrace, trace, ya, 'y');
}
};
function calcOneAxis(calcTrace, trace, axis, coord) {
var opts = trace['error_' + coord] || {},
isVisible = (opts.visible && ['linear', 'log'].indexOf(axis.type) !== -1),
vals = [];
if(!isVisible) return;
var computeError = makeComputeError(opts);
for(var i = 0; i < calcTrace.length; i++) {
var calcPt = calcTrace[i],
calcCoord = calcPt[coord];
if(!isNumeric(axis.c2l(calcCoord))) continue;
var errors = computeError(calcCoord, i);
if(isNumeric(errors[0]) && isNumeric(errors[1])) {
var shoe = calcPt[coord + 's'] = calcCoord - errors[0],
hat = calcPt[coord + 'h'] = calcCoord + errors[1];
vals.push(shoe, hat);
}
}
Axes.expand(axis, vals, {padded: true});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Error bar computing function generator
*
* N.B. The generated function does not clean the dataPt entries. Non-numeric
* entries result in undefined error magnitudes.
*
* @param {object} opts error bar attributes
*
* @return {function} :
* @param {numeric} dataPt data point from where to compute the error magnitude
* @param {number} index index of dataPt in its corresponding data array
* @return {array}
* - error[0] : error magnitude in the negative direction
* - error[1] : " " " " positive "
*/
module.exports = function makeComputeError(opts) {
var type = opts.type,
symmetric = opts.symmetric;
if(type === 'data') {
var array = opts.array,
arrayminus = opts.arrayminus;
if(symmetric || arrayminus === undefined) {
return function computeError(dataPt, index) {
var val = +(array[index]);
return [val, val];
};
}
else {
return function computeError(dataPt, index) {
return [+arrayminus[index], +array[index]];
};
}
}
else {
var computeErrorValue = makeComputeErrorValue(type, opts.value),
computeErrorValueMinus = makeComputeErrorValue(type, opts.valueminus);
if(symmetric || opts.valueminus === undefined) {
return function computeError(dataPt) {
var val = computeErrorValue(dataPt);
return [val, val];
};
}
else {
return function computeError(dataPt) {
return [
computeErrorValueMinus(dataPt),
computeErrorValue(dataPt)
];
};
}
}
};
/**
* Compute error bar magnitude (for all types except data)
*
* @param {string} type error bar type
* @param {numeric} value error bar value
*
* @return {function} :
* @param {numeric} dataPt
*/
function makeComputeErrorValue(type, value) {
if(type === 'percent') {
return function(dataPt) {
return Math.abs(dataPt * value / 100);
};
}
if(type === 'constant') {
return function() {
return Math.abs(value);
};
}
if(type === 'sqrt') {
return function(dataPt) {
return Math.sqrt(Math.abs(dataPt));
};
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Lib = require('../../lib');
var attributes = require('./attributes');
module.exports = function(traceIn, traceOut, defaultColor, opts) {
var objName = 'error_' + opts.axis,
containerOut = traceOut[objName] = {},
containerIn = traceIn[objName] || {};
function coerce(attr, dflt) {
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
}
var hasErrorBars = (
containerIn.array !== undefined ||
containerIn.value !== undefined ||
containerIn.type === 'sqrt'
);
var visible = coerce('visible', hasErrorBars);
if(visible === false) return;
var type = coerce('type', 'array' in containerIn ? 'data' : 'percent'),
symmetric = true;
if(type !== 'sqrt') {
symmetric = coerce('symmetric',
!((type === 'data' ? 'arrayminus' : 'valueminus') in containerIn));
}
if(type === 'data') {
var array = coerce('array');
if(!array) containerOut.array = [];
coerce('traceref');
if(!symmetric) {
var arrayminus = coerce('arrayminus');
if(!arrayminus) containerOut.arrayminus = [];
coerce('tracerefminus');
}
}
else if(type === 'percent' || type === 'constant') {
coerce('value');
if(!symmetric) coerce('valueminus');
}
var copyAttr = 'copy_' + opts.inherit + 'style';
if(opts.inherit) {
var inheritObj = traceOut['error_' + opts.inherit];
if((inheritObj || {}).visible) {
coerce(copyAttr, !(containerIn.color ||
isNumeric(containerIn.thickness) ||
isNumeric(containerIn.width)));
}
}
if(!opts.inherit || !containerOut[copyAttr]) {
coerce('color', defaultColor);
coerce('thickness');
coerce('width', Registry.traceIs(traceOut, 'gl3d') ? 0 : 4);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var errorBars = module.exports = {};
errorBars.attributes = require('./attributes');
errorBars.supplyDefaults = require('./defaults');
errorBars.calc = require('./calc');
errorBars.calcFromTrace = function(trace, layout) {
var x = trace.x || [],
y = trace.y || [],
len = x.length || y.length;
var calcdataMock = new Array(len);
for(var i = 0; i < len; i++) {
calcdataMock[i] = {
x: x[i],
y: y[i]
};
}
calcdataMock[0].trace = trace;
errorBars.calc({
calcdata: [calcdataMock],
_fullLayout: layout
});
return calcdataMock;
};
errorBars.plot = require('./plot');
errorBars.style = require('./style');
errorBars.hoverInfo = function(calcPoint, trace, hoverPoint) {
if((trace.error_y || {}).visible) {
hoverPoint.yerr = calcPoint.yh - calcPoint.y;
if(!trace.error_y.symmetric) hoverPoint.yerrneg = calcPoint.y - calcPoint.ys;
}
if((trace.error_x || {}).visible) {
hoverPoint.xerr = calcPoint.xh - calcPoint.x;
if(!trace.error_x.symmetric) hoverPoint.xerrneg = calcPoint.x - calcPoint.xs;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var subTypes = require('../../traces/scatter/subtypes');
module.exports = function plot(traces, plotinfo, transitionOpts) {
var isNew;
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
var hasAnimation = transitionOpts && transitionOpts.duration > 0;
traces.each(function(d) {
var trace = d[0].trace,
// || {} is in case the trace (specifically scatterternary)
// doesn't support error bars at all, but does go through
// the scatter.plot mechanics, which calls ErrorBars.plot
// internally
xObj = trace.error_x || {},
yObj = trace.error_y || {};
var keyFunc;
if(trace.ids) {
keyFunc = function(d) {return d.id;};
}
var sparse = (
subTypes.hasMarkers(trace) &&
trace.marker.maxdisplayed > 0
);
if(!yObj.visible && !xObj.visible) return;
var errorbars = d3.select(this).selectAll('g.errorbar')
.data(d, keyFunc);
errorbars.exit().remove();
errorbars.style('opacity', 1);
var enter = errorbars.enter().append('g')
.classed('errorbar', true);
if(hasAnimation) {
enter.style('opacity', 0).transition()
.duration(transitionOpts.duration)
.style('opacity', 1);
}
errorbars.each(function(d) {
var errorbar = d3.select(this);
var coords = errorCoords(d, xa, ya);
if(sparse && !d.vis) return;
var path;
if(yObj.visible && isNumeric(coords.x) &&
isNumeric(coords.yh) &&
isNumeric(coords.ys)) {
var yw = yObj.width;
path = 'M' + (coords.x - yw) + ',' +
coords.yh + 'h' + (2 * yw) + // hat
'm-' + yw + ',0V' + coords.ys; // bar
if(!coords.noYS) path += 'm-' + yw + ',0h' + (2 * yw); // shoe
var yerror = errorbar.select('path.yerror');
isNew = !yerror.size();
if(isNew) {
yerror = errorbar.append('path')
.classed('yerror', true);
} else if(hasAnimation) {
yerror = yerror
.transition()
.duration(transitionOpts.duration)
.ease(transitionOpts.easing);
}
yerror.attr('d', path);
}
if(xObj.visible && isNumeric(coords.y) &&
isNumeric(coords.xh) &&
isNumeric(coords.xs)) {
var xw = (xObj.copy_ystyle ? yObj : xObj).width;
path = 'M' + coords.xh + ',' +
(coords.y - xw) + 'v' + (2 * xw) + // hat
'm0,-' + xw + 'H' + coords.xs; // bar
if(!coords.noXS) path += 'm0,-' + xw + 'v' + (2 * xw); // shoe
var xerror = errorbar.select('path.xerror');
isNew = !xerror.size();
if(isNew) {
xerror = errorbar.append('path')
.classed('xerror', true);
} else if(hasAnimation) {
xerror = xerror
.transition()
.duration(transitionOpts.duration)
.ease(transitionOpts.easing);
}
xerror.attr('d', path);
}
});
});
};
// compute the coordinates of the error-bar objects
function errorCoords(d, xa, ya) {
var out = {
x: xa.c2p(d.x),
y: ya.c2p(d.y)
};
// calculate the error bar size and hat and shoe locations
if(d.yh !== undefined) {
out.yh = ya.c2p(d.yh);
out.ys = ya.c2p(d.ys);
// if the shoes go off-scale (ie log scale, error bars past zero)
// clip the bar and hide the shoes
if(!isNumeric(out.ys)) {
out.noYS = true;
out.ys = ya.c2p(d.ys, true);
}
}
if(d.xh !== undefined) {
out.xh = xa.c2p(d.xh);
out.xs = xa.c2p(d.xs);
if(!isNumeric(out.xs)) {
out.noXS = true;
out.xs = xa.c2p(d.xs, true);
}
}
return out;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Color = require('../color');
module.exports = function style(traces) {
traces.each(function(d) {
var trace = d[0].trace,
yObj = trace.error_y || {},
xObj = trace.error_x || {};
var s = d3.select(this);
s.selectAll('path.yerror')
.style('stroke-width', yObj.thickness + 'px')
.call(Color.stroke, yObj.color);
if(xObj.copy_ystyle) xObj = yObj;
s.selectAll('path.xerror')
.style('stroke-width', xObj.thickness + 'px')
.call(Color.stroke, xObj.color);
});
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| anchor_utils.js | 50% | (4 / 8) | 0% | (0 / 14) | 0% | (0 / 4) | 50% | (4 / 8) | |
| get_legend_data.js | 9.09% | (4 / 44) | 0% | (0 / 26) | 0% | (0 / 2) | 9.76% | (4 / 41) | |
| helpers.js | 55.56% | (5 / 9) | 0% | (0 / 6) | 0% | (0 / 4) | 55.56% | (5 / 9) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1 1 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /** * Determine the position anchor property of x/y xanchor/yanchor components. * * - values < 1/3 align the low side at that fraction, * - values [1/3, 2/3] align the center at that fraction, * - values > 2/3 align the right at that fraction. */ exports.isRightAnchor = function isRightAnchor(opts) { return ( opts.xanchor === 'right' || (opts.xanchor === 'auto' && opts.x >= 2 / 3) ); }; exports.isCenterAnchor = function isCenterAnchor(opts) { return ( opts.xanchor === 'center' || (opts.xanchor === 'auto' && opts.x > 1 / 3 && opts.x < 2 / 3) ); }; exports.isBottomAnchor = function isBottomAnchor(opts) { return ( opts.yanchor === 'bottom' || (opts.yanchor === 'auto' && opts.y <= 1 / 3) ); }; exports.isMiddleAnchor = function isMiddleAnchor(opts) { return ( opts.yanchor === 'middle' || (opts.yanchor === 'auto' && opts.y > 1 / 3 && opts.y < 2 / 3) ); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var helpers = require('./helpers');
module.exports = function getLegendData(calcdata, opts) {
var lgroupToTraces = {},
lgroups = [],
hasOneNonBlankGroup = false,
slicesShown = {},
lgroupi = 0;
var i, j;
function addOneItem(legendGroup, legendItem) {
// each '' legend group is treated as a separate group
if(legendGroup === '' || !helpers.isGrouped(opts)) {
var uniqueGroup = '~~i' + lgroupi; // TODO: check this against fullData legendgroups?
lgroups.push(uniqueGroup);
lgroupToTraces[uniqueGroup] = [[legendItem]];
lgroupi++;
}
else if(lgroups.indexOf(legendGroup) === -1) {
lgroups.push(legendGroup);
hasOneNonBlankGroup = true;
lgroupToTraces[legendGroup] = [[legendItem]];
}
else lgroupToTraces[legendGroup].push([legendItem]);
}
// build an { legendgroup: [cd0, cd0], ... } object
for(i = 0; i < calcdata.length; i++) {
var cd = calcdata[i],
cd0 = cd[0],
trace = cd0.trace,
lgroup = trace.legendgroup;
if(!helpers.legendGetsTrace(trace) || !trace.showlegend) continue;
if(Registry.traceIs(trace, 'pie')) {
if(!slicesShown[lgroup]) slicesShown[lgroup] = {};
for(j = 0; j < cd.length; j++) {
var labelj = cd[j].label;
if(!slicesShown[lgroup][labelj]) {
addOneItem(lgroup, {
label: labelj,
color: cd[j].color,
i: cd[j].i,
trace: trace
});
slicesShown[lgroup][labelj] = true;
}
}
}
else addOneItem(lgroup, cd0);
}
// won't draw a legend in this case
if(!lgroups.length) return [];
// rearrange lgroupToTraces into a d3-friendly array of arrays
var lgroupsLength = lgroups.length,
ltraces,
legendData;
if(hasOneNonBlankGroup && helpers.isGrouped(opts)) {
legendData = new Array(lgroupsLength);
for(i = 0; i < lgroupsLength; i++) {
ltraces = lgroupToTraces[lgroups[i]];
legendData[i] = helpers.isReversed(opts) ? ltraces.reverse() : ltraces;
}
}
else {
// collapse all groups into one if all groups are blank
legendData = [new Array(lgroupsLength)];
for(i = 0; i < lgroupsLength; i++) {
ltraces = lgroupToTraces[lgroups[i]][0];
legendData[0][helpers.isReversed(opts) ? lgroupsLength - i - 1 : i] = ltraces;
}
lgroupsLength = 1;
}
// needed in repositionLegend
opts._lgroupsLength = lgroupsLength;
return legendData;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
exports.legendGetsTrace = function legendGetsTrace(trace) {
return trace.visible && Registry.traceIs(trace, 'showLegend');
};
exports.isGrouped = function isGrouped(legendLayout) {
return (legendLayout.traceorder || '').indexOf('grouped') !== -1;
};
exports.isVertical = function isVertical(legendLayout) {
return legendLayout.orientation !== 'h';
};
exports.isReversed = function isReversed(legendLayout) {
return (legendLayout.traceorder || '').indexOf('reversed') !== -1;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| buttons.js | 26.42% | (42 / 159) | 0% | (0 / 71) | 0% | (0 / 15) | 26.75% | (42 / 157) | |
| index.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| manage.js | 10.68% | (11 / 103) | 0% | (0 / 79) | 0% | (0 / 7) | 11.22% | (11 / 98) | |
| modebar.js | 13.39% | (17 / 127) | 0% | (0 / 56) | 0% | (0 / 16) | 14.41% | (17 / 118) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../../plotly');
var Plots = require('../../plots/plots');
var Axes = require('../../plots/cartesian/axes');
var Lib = require('../../lib');
var downloadImage = require('../../snapshot/download');
var Icons = require('../../../build/ploticon');
var modeBarButtons = module.exports = {};
/**
* ModeBar buttons configuration
*
* @param {string} name
* name / id of the buttons (for tracking)
* @param {string} title
* text that appears while hovering over the button,
* enter null, false or '' for no hover text
* @param {string} icon
* svg icon object associated with the button
* can be linked to Plotly.Icons to use the default plotly icons
* @param {string} [gravity]
* icon positioning
* @param {function} click
* click handler associated with the button, a function of
* 'gd' (the main graph object) and
* 'ev' (the event object)
* @param {string} [attr]
* attribute associated with button,
* use this with 'val' to keep track of the state
* @param {*} [val]
* initial 'attr' value, can be a function of gd
* @param {boolean} [toggle]
* is the button a toggle button?
*/
modeBarButtons.toImage = {
name: 'toImage',
title: 'Download plot as a png',
icon: Icons.camera,
click: function(gd) {
var format = 'png';
Lib.notifier('Taking snapshot - this may take a few seconds', 'long');
if(Lib.isIE()) {
Lib.notifier('IE only supports svg. Changing format to svg.', 'long');
format = 'svg';
}
downloadImage(gd, {'format': format})
.then(function(filename) {
Lib.notifier('Snapshot succeeded - ' + filename, 'long');
})
.catch(function() {
Lib.notifier('Sorry there was a problem downloading your snapshot!', 'long');
});
}
};
modeBarButtons.sendDataToCloud = {
name: 'sendDataToCloud',
title: 'Save and edit plot in cloud',
icon: Icons.disk,
click: function(gd) {
Plots.sendDataToCloud(gd);
}
};
modeBarButtons.zoom2d = {
name: 'zoom2d',
title: 'Zoom',
attr: 'dragmode',
val: 'zoom',
icon: Icons.zoombox,
click: handleCartesian
};
modeBarButtons.pan2d = {
name: 'pan2d',
title: 'Pan',
attr: 'dragmode',
val: 'pan',
icon: Icons.pan,
click: handleCartesian
};
modeBarButtons.select2d = {
name: 'select2d',
title: 'Box Select',
attr: 'dragmode',
val: 'select',
icon: Icons.selectbox,
click: handleCartesian
};
modeBarButtons.lasso2d = {
name: 'lasso2d',
title: 'Lasso Select',
attr: 'dragmode',
val: 'lasso',
icon: Icons.lasso,
click: handleCartesian
};
modeBarButtons.zoomIn2d = {
name: 'zoomIn2d',
title: 'Zoom in',
attr: 'zoom',
val: 'in',
icon: Icons.zoom_plus,
click: handleCartesian
};
modeBarButtons.zoomOut2d = {
name: 'zoomOut2d',
title: 'Zoom out',
attr: 'zoom',
val: 'out',
icon: Icons.zoom_minus,
click: handleCartesian
};
modeBarButtons.autoScale2d = {
name: 'autoScale2d',
title: 'Autoscale',
attr: 'zoom',
val: 'auto',
icon: Icons.autoscale,
click: handleCartesian
};
modeBarButtons.resetScale2d = {
name: 'resetScale2d',
title: 'Reset axes',
attr: 'zoom',
val: 'reset',
icon: Icons.home,
click: handleCartesian
};
modeBarButtons.hoverClosestCartesian = {
name: 'hoverClosestCartesian',
title: 'Show closest data on hover',
attr: 'hovermode',
val: 'closest',
icon: Icons.tooltip_basic,
gravity: 'ne',
click: handleCartesian
};
modeBarButtons.hoverCompareCartesian = {
name: 'hoverCompareCartesian',
title: 'Compare data on hover',
attr: 'hovermode',
val: function(gd) {
return gd._fullLayout._isHoriz ? 'y' : 'x';
},
icon: Icons.tooltip_compare,
gravity: 'ne',
click: handleCartesian
};
function handleCartesian(gd, ev) {
var button = ev.currentTarget,
astr = button.getAttribute('data-attr'),
val = button.getAttribute('data-val') || true,
fullLayout = gd._fullLayout,
aobj = {},
axList = Axes.list(gd, null, true),
ax,
allEnabled = 'on',
i;
if(astr === 'zoom') {
var mag = (val === 'in') ? 0.5 : 2,
r0 = (1 + mag) / 2,
r1 = (1 - mag) / 2;
var axName;
for(i = 0; i < axList.length; i++) {
ax = axList[i];
if(!ax.fixedrange) {
axName = ax._name;
if(val === 'auto') aobj[axName + '.autorange'] = true;
else if(val === 'reset') {
if(ax._rangeInitial === undefined) {
aobj[axName + '.autorange'] = true;
}
else {
var rangeInitial = ax._rangeInitial.slice();
aobj[axName + '.range[0]'] = rangeInitial[0];
aobj[axName + '.range[1]'] = rangeInitial[1];
}
if(ax._showSpikeInitial !== undefined) {
aobj[axName + '.showspikes'] = ax._showSpikeInitial;
if(allEnabled === 'on' && !ax._showSpikeInitial) {
allEnabled = 'off';
}
}
}
else {
var rangeNow = [
ax.r2l(ax.range[0]),
ax.r2l(ax.range[1]),
];
var rangeNew = [
r0 * rangeNow[0] + r1 * rangeNow[1],
r0 * rangeNow[1] + r1 * rangeNow[0]
];
aobj[axName + '.range[0]'] = ax.l2r(rangeNew[0]);
aobj[axName + '.range[1]'] = ax.l2r(rangeNew[1]);
}
}
}
fullLayout._cartesianSpikesEnabled = allEnabled;
}
else {
// if ALL traces have orientation 'h', 'hovermode': 'x' otherwise: 'y'
if(astr === 'hovermode' && (val === 'x' || val === 'y')) {
val = fullLayout._isHoriz ? 'y' : 'x';
button.setAttribute('data-val', val);
if(val !== 'closest') {
fullLayout._cartesianSpikesEnabled = 'off';
}
} else if(astr === 'hovermode' && val === 'closest') {
for(i = 0; i < axList.length; i++) {
ax = axList[i];
if(allEnabled === 'on' && !ax.showspikes) {
allEnabled = 'off';
}
}
fullLayout._cartesianSpikesEnabled = allEnabled;
}
aobj[astr] = val;
}
Plotly.relayout(gd, aobj);
}
modeBarButtons.zoom3d = {
name: 'zoom3d',
title: 'Zoom',
attr: 'scene.dragmode',
val: 'zoom',
icon: Icons.zoombox,
click: handleDrag3d
};
modeBarButtons.pan3d = {
name: 'pan3d',
title: 'Pan',
attr: 'scene.dragmode',
val: 'pan',
icon: Icons.pan,
click: handleDrag3d
};
modeBarButtons.orbitRotation = {
name: 'orbitRotation',
title: 'orbital rotation',
attr: 'scene.dragmode',
val: 'orbit',
icon: Icons['3d_rotate'],
click: handleDrag3d
};
modeBarButtons.tableRotation = {
name: 'tableRotation',
title: 'turntable rotation',
attr: 'scene.dragmode',
val: 'turntable',
icon: Icons['z-axis'],
click: handleDrag3d
};
function handleDrag3d(gd, ev) {
var button = ev.currentTarget,
attr = button.getAttribute('data-attr'),
val = button.getAttribute('data-val') || true,
fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d'),
layoutUpdate = {};
var parts = attr.split('.');
for(var i = 0; i < sceneIds.length; i++) {
layoutUpdate[sceneIds[i] + '.' + parts[1]] = val;
}
Plotly.relayout(gd, layoutUpdate);
}
modeBarButtons.resetCameraDefault3d = {
name: 'resetCameraDefault3d',
title: 'Reset camera to default',
attr: 'resetDefault',
icon: Icons.home,
click: handleCamera3d
};
modeBarButtons.resetCameraLastSave3d = {
name: 'resetCameraLastSave3d',
title: 'Reset camera to last save',
attr: 'resetLastSave',
icon: Icons.movie,
click: handleCamera3d
};
function handleCamera3d(gd, ev) {
var button = ev.currentTarget,
attr = button.getAttribute('data-attr'),
fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d'),
aobj = {};
for(var i = 0; i < sceneIds.length; i++) {
var sceneId = sceneIds[i],
key = sceneId + '.camera',
scene = fullLayout[sceneId]._scene;
if(attr === 'resetDefault') {
aobj[key] = null;
}
else if(attr === 'resetLastSave') {
aobj[key] = Lib.extendDeep({}, scene.cameraInitial);
}
}
Plotly.relayout(gd, aobj);
}
modeBarButtons.hoverClosest3d = {
name: 'hoverClosest3d',
title: 'Toggle show closest data on hover',
attr: 'hovermode',
val: null,
toggle: true,
icon: Icons.tooltip_basic,
gravity: 'ne',
click: handleHover3d
};
function handleHover3d(gd, ev) {
var button = ev.currentTarget,
val = button._previousVal || false,
layout = gd.layout,
fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');
var axes = ['xaxis', 'yaxis', 'zaxis'],
spikeAttrs = ['showspikes', 'spikesides', 'spikethickness', 'spikecolor'];
// initialize 'current spike' object to be stored in the DOM
var currentSpikes = {},
axisSpikes = {},
layoutUpdate = {};
if(val) {
layoutUpdate = Lib.extendDeep(layout, val);
button._previousVal = null;
}
else {
layoutUpdate = {
'allaxes.showspikes': false
};
for(var i = 0; i < sceneIds.length; i++) {
var sceneId = sceneIds[i],
sceneLayout = fullLayout[sceneId],
sceneSpikes = currentSpikes[sceneId] = {};
sceneSpikes.hovermode = sceneLayout.hovermode;
layoutUpdate[sceneId + '.hovermode'] = false;
// copy all the current spike attrs
for(var j = 0; j < 3; j++) {
var axis = axes[j];
axisSpikes = sceneSpikes[axis] = {};
for(var k = 0; k < spikeAttrs.length; k++) {
var spikeAttr = spikeAttrs[k];
axisSpikes[spikeAttr] = sceneLayout[axis][spikeAttr];
}
}
}
button._previousVal = Lib.extendDeep({}, currentSpikes);
}
Plotly.relayout(gd, layoutUpdate);
}
modeBarButtons.zoomInGeo = {
name: 'zoomInGeo',
title: 'Zoom in',
attr: 'zoom',
val: 'in',
icon: Icons.zoom_plus,
click: handleGeo
};
modeBarButtons.zoomOutGeo = {
name: 'zoomOutGeo',
title: 'Zoom out',
attr: 'zoom',
val: 'out',
icon: Icons.zoom_minus,
click: handleGeo
};
modeBarButtons.resetGeo = {
name: 'resetGeo',
title: 'Reset',
attr: 'reset',
val: null,
icon: Icons.autoscale,
click: handleGeo
};
modeBarButtons.hoverClosestGeo = {
name: 'hoverClosestGeo',
title: 'Toggle show closest data on hover',
attr: 'hovermode',
val: null,
toggle: true,
icon: Icons.tooltip_basic,
gravity: 'ne',
click: toggleHover
};
function handleGeo(gd, ev) {
var button = ev.currentTarget,
attr = button.getAttribute('data-attr'),
val = button.getAttribute('data-val') || true,
fullLayout = gd._fullLayout,
geoIds = Plots.getSubplotIds(fullLayout, 'geo');
for(var i = 0; i < geoIds.length; i++) {
var geo = fullLayout[geoIds[i]]._subplot;
if(attr === 'zoom') {
var scale = geo.projection.scale();
var newScale = (val === 'in') ? 2 * scale : 0.5 * scale;
geo.projection.scale(newScale);
geo.zoom.scale(newScale);
geo.render();
}
else if(attr === 'reset') geo.zoomReset();
}
}
modeBarButtons.hoverClosestGl2d = {
name: 'hoverClosestGl2d',
title: 'Toggle show closest data on hover',
attr: 'hovermode',
val: null,
toggle: true,
icon: Icons.tooltip_basic,
gravity: 'ne',
click: toggleHover
};
modeBarButtons.hoverClosestPie = {
name: 'hoverClosestPie',
title: 'Toggle show closest data on hover',
attr: 'hovermode',
val: 'closest',
icon: Icons.tooltip_basic,
gravity: 'ne',
click: toggleHover
};
function toggleHover(gd) {
var fullLayout = gd._fullLayout;
var onHoverVal;
if(fullLayout._has('cartesian')) {
onHoverVal = fullLayout._isHoriz ? 'y' : 'x';
}
else onHoverVal = 'closest';
var newHover = gd._fullLayout.hovermode ? false : onHoverVal;
Plotly.relayout(gd, 'hovermode', newHover);
}
// buttons when more then one plot types are present
modeBarButtons.toggleHover = {
name: 'toggleHover',
title: 'Toggle show closest data on hover',
attr: 'hovermode',
val: null,
toggle: true,
icon: Icons.tooltip_basic,
gravity: 'ne',
click: function(gd, ev) {
toggleHover(gd);
// the 3d hovermode update must come
// last so that layout.hovermode update does not
// override scene?.hovermode?.layout.
handleHover3d(gd, ev);
}
};
modeBarButtons.resetViews = {
name: 'resetViews',
title: 'Reset views',
icon: Icons.home,
click: function(gd, ev) {
var button = ev.currentTarget;
button.setAttribute('data-attr', 'zoom');
button.setAttribute('data-val', 'reset');
handleCartesian(gd, ev);
button.setAttribute('data-attr', 'resetLastSave');
handleCamera3d(gd, ev);
// N.B handleCamera3d also triggers a replot for
// geo subplots.
}
};
modeBarButtons.toggleSpikelines = {
name: 'toggleSpikelines',
title: 'Toggle Spike Lines',
icon: Icons.spikeline,
attr: '_cartesianSpikesEnabled',
val: 'on',
click: function(gd) {
var fullLayout = gd._fullLayout;
fullLayout._cartesianSpikesEnabled = fullLayout.hovermode === 'closest' ?
(fullLayout._cartesianSpikesEnabled === 'on' ? 'off' : 'on') : 'on';
var aobj = setSpikelineVisibility(gd);
aobj.hovermode = 'closest';
Plotly.relayout(gd, aobj);
}
};
function setSpikelineVisibility(gd) {
var fullLayout = gd._fullLayout,
axList = Axes.list(gd, null, true),
ax,
axName,
aobj = {};
for(var i = 0; i < axList.length; i++) {
ax = axList[i];
axName = ax._name;
aobj[axName + '.showspikes'] = fullLayout._cartesianSpikesEnabled === 'on' ? true : false;
}
return aobj;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
exports.manage = require('./manage');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Axes = require('../../plots/cartesian/axes');
var scatterSubTypes = require('../../traces/scatter/subtypes');
var createModeBar = require('./modebar');
var modeBarButtons = require('./buttons');
/**
* ModeBar wrapper around 'create' and 'update',
* chooses buttons to pass to ModeBar constructor based on
* plot type and plot config.
*
* @param {object} gd main plot object
*
*/
module.exports = function manageModeBar(gd) {
var fullLayout = gd._fullLayout,
context = gd._context,
modeBar = fullLayout._modeBar;
if(!context.displayModeBar) {
if(modeBar) {
modeBar.destroy();
delete fullLayout._modeBar;
}
return;
}
if(!Array.isArray(context.modeBarButtonsToRemove)) {
throw new Error([
'*modeBarButtonsToRemove* configuration options',
'must be an array.'
].join(' '));
}
if(!Array.isArray(context.modeBarButtonsToAdd)) {
throw new Error([
'*modeBarButtonsToAdd* configuration options',
'must be an array.'
].join(' '));
}
var customButtons = context.modeBarButtons;
var buttonGroups;
if(Array.isArray(customButtons) && customButtons.length) {
buttonGroups = fillCustomButton(customButtons);
}
else {
buttonGroups = getButtonGroups(
gd,
context.modeBarButtonsToRemove,
context.modeBarButtonsToAdd
);
}
if(modeBar) modeBar.update(gd, buttonGroups);
else fullLayout._modeBar = createModeBar(gd, buttonGroups);
};
// logic behind which buttons are displayed by default
function getButtonGroups(gd, buttonsToRemove, buttonsToAdd) {
var fullLayout = gd._fullLayout,
fullData = gd._fullData;
var hasCartesian = fullLayout._has('cartesian'),
hasGL3D = fullLayout._has('gl3d'),
hasGeo = fullLayout._has('geo'),
hasPie = fullLayout._has('pie'),
hasGL2D = fullLayout._has('gl2d'),
hasTernary = fullLayout._has('ternary');
var groups = [];
function addGroup(newGroup) {
var out = [];
for(var i = 0; i < newGroup.length; i++) {
var button = newGroup[i];
if(buttonsToRemove.indexOf(button) !== -1) continue;
out.push(modeBarButtons[button]);
}
groups.push(out);
}
// buttons common to all plot types
addGroup(['toImage', 'sendDataToCloud']);
// graphs with more than one plot types get 'union buttons'
// which reset the view or toggle hover labels across all subplots.
if((hasCartesian || hasGL2D || hasPie || hasTernary) + hasGeo + hasGL3D > 1) {
addGroup(['resetViews', 'toggleHover']);
return appendButtonsToGroups(groups, buttonsToAdd);
}
if(hasGL3D) {
addGroup(['zoom3d', 'pan3d', 'orbitRotation', 'tableRotation']);
addGroup(['resetCameraDefault3d', 'resetCameraLastSave3d']);
addGroup(['hoverClosest3d']);
}
if(hasGeo) {
addGroup(['zoomInGeo', 'zoomOutGeo', 'resetGeo']);
addGroup(['hoverClosestGeo']);
}
var allAxesFixed = areAllAxesFixed(fullLayout),
dragModeGroup = [];
if(((hasCartesian || hasGL2D) && !allAxesFixed) || hasTernary) {
dragModeGroup = ['zoom2d', 'pan2d'];
}
if((hasCartesian || hasTernary) && isSelectable(fullData)) {
dragModeGroup.push('select2d');
dragModeGroup.push('lasso2d');
}
if(dragModeGroup.length) addGroup(dragModeGroup);
if((hasCartesian || hasGL2D) && !allAxesFixed && !hasTernary) {
addGroup(['zoomIn2d', 'zoomOut2d', 'autoScale2d', 'resetScale2d']);
}
if(hasCartesian && hasPie) {
addGroup(['toggleHover']);
}
else if(hasGL2D) {
addGroup(['hoverClosestGl2d']);
}
else if(hasCartesian) {
addGroup(['toggleSpikelines', 'hoverClosestCartesian', 'hoverCompareCartesian']);
}
else if(hasPie) {
addGroup(['hoverClosestPie']);
}
return appendButtonsToGroups(groups, buttonsToAdd);
}
function areAllAxesFixed(fullLayout) {
var axList = Axes.list({_fullLayout: fullLayout}, null, true);
var allFixed = true;
for(var i = 0; i < axList.length; i++) {
if(!axList[i].fixedrange) {
allFixed = false;
break;
}
}
return allFixed;
}
// look for traces that support selection
// to be updated as we add more selectPoints handlers
function isSelectable(fullData) {
var selectable = false;
for(var i = 0; i < fullData.length; i++) {
if(selectable) break;
var trace = fullData[i];
if(!trace._module || !trace._module.selectPoints) continue;
if(trace.type === 'scatter' || trace.type === 'scatterternary') {
if(scatterSubTypes.hasMarkers(trace) || scatterSubTypes.hasText(trace)) {
selectable = true;
}
}
// assume that in general if the trace module has selectPoints,
// then it's selectable. Scatter is an exception to this because it must
// have markers or text, not just be a scatter type.
else selectable = true;
}
return selectable;
}
function appendButtonsToGroups(groups, buttons) {
if(buttons.length) {
if(Array.isArray(buttons[0])) {
for(var i = 0; i < buttons.length; i++) {
groups.push(buttons[i]);
}
}
else groups.push(buttons);
}
return groups;
}
// fill in custom buttons referring to default mode bar buttons
function fillCustomButton(customButtons) {
for(var i = 0; i < customButtons.length; i++) {
var buttonGroup = customButtons[i];
for(var j = 0; j < buttonGroup.length; j++) {
var button = buttonGroup[j];
if(typeof button === 'string') {
if(modeBarButtons[button] !== undefined) {
customButtons[i][j] = modeBarButtons[button];
}
else {
throw new Error([
'*modeBarButtons* configuration options',
'invalid button name'
].join(' '));
}
}
}
}
return customButtons;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Lib = require('../../lib');
var Icons = require('../../../build/ploticon');
/**
* UI controller for interactive plots
* @Class
* @Param {object} opts
* @Param {object} opts.buttons nested arrays of grouped buttons config objects
* @Param {object} opts.container container div to append modeBar
* @Param {object} opts.graphInfo primary plot object containing data and layout
*/
function ModeBar(opts) {
this.container = opts.container;
this.element = document.createElement('div');
this.update(opts.graphInfo, opts.buttons);
this.container.appendChild(this.element);
}
var proto = ModeBar.prototype;
/**
* Update modeBar (buttons and logo)
*
* @param {object} graphInfo primary plot object containing data and layout
* @param {array of arrays} buttons nested arrays of grouped buttons to initialize
*
*/
proto.update = function(graphInfo, buttons) {
this.graphInfo = graphInfo;
var context = this.graphInfo._context;
if(context.displayModeBar === 'hover') {
this.element.className = 'modebar modebar--hover';
}
else this.element.className = 'modebar';
// if buttons or logo have changed, redraw modebar interior
var needsNewButtons = !this.hasButtons(buttons),
needsNewLogo = (this.hasLogo !== context.displaylogo);
if(needsNewButtons || needsNewLogo) {
this.removeAllButtons();
this.updateButtons(buttons);
if(context.displaylogo) {
this.element.appendChild(this.getLogo());
this.hasLogo = true;
}
}
this.updateActiveButton();
};
proto.updateButtons = function(buttons) {
var _this = this;
this.buttons = buttons;
this.buttonElements = [];
this.buttonsNames = [];
this.buttons.forEach(function(buttonGroup) {
var group = _this.createGroup();
buttonGroup.forEach(function(buttonConfig) {
var buttonName = buttonConfig.name;
if(!buttonName) {
throw new Error('must provide button \'name\' in button config');
}
if(_this.buttonsNames.indexOf(buttonName) !== -1) {
throw new Error('button name \'' + buttonName + '\' is taken');
}
_this.buttonsNames.push(buttonName);
var button = _this.createButton(buttonConfig);
_this.buttonElements.push(button);
group.appendChild(button);
});
_this.element.appendChild(group);
});
};
/**
* Empty div for containing a group of buttons
* @Return {HTMLelement}
*/
proto.createGroup = function() {
var group = document.createElement('div');
group.className = 'modebar-group';
return group;
};
/**
* Create a new button div and set constant and configurable attributes
* @Param {object} config (see ./buttons.js for more info)
* @Return {HTMLelement}
*/
proto.createButton = function(config) {
var _this = this,
button = document.createElement('a');
button.setAttribute('rel', 'tooltip');
button.className = 'modebar-btn';
var title = config.title;
if(title === undefined) title = config.name;
if(title || title === 0) button.setAttribute('data-title', title);
if(config.attr !== undefined) button.setAttribute('data-attr', config.attr);
var val = config.val;
if(val !== undefined) {
if(typeof val === 'function') val = val(this.graphInfo);
button.setAttribute('data-val', val);
}
var click = config.click;
if(typeof click !== 'function') {
throw new Error('must provide button \'click\' function in button config');
}
else {
button.addEventListener('click', function(ev) {
config.click(_this.graphInfo, ev);
// only needed for 'hoverClosestGeo' which does not call relayout
_this.updateActiveButton(ev.currentTarget);
});
}
button.setAttribute('data-toggle', config.toggle || false);
if(config.toggle) d3.select(button).classed('active', true);
button.appendChild(this.createIcon(config.icon || Icons.question, config.name));
button.setAttribute('data-gravity', config.gravity || 'n');
return button;
};
/**
* Add an icon to a button
* @Param {object} thisIcon
* @Param {number} thisIcon.width
* @Param {string} thisIcon.path
* @Return {HTMLelement}
*/
proto.createIcon = function(thisIcon, name) {
var iconHeight = thisIcon.ascent - thisIcon.descent,
svgNS = 'http://www.w3.org/2000/svg',
icon = document.createElementNS(svgNS, 'svg'),
path = document.createElementNS(svgNS, 'path');
icon.setAttribute('height', '1em');
icon.setAttribute('width', (thisIcon.width / iconHeight) + 'em');
icon.setAttribute('viewBox', [0, 0, thisIcon.width, iconHeight].join(' '));
var transform = name === 'toggleSpikelines' ?
'matrix(1.5 0 0 -1.5 0 ' + thisIcon.ascent + ')' :
'matrix(1 0 0 -1 0 ' + thisIcon.ascent + ')';
path.setAttribute('d', thisIcon.path);
path.setAttribute('transform', transform);
icon.appendChild(path);
return icon;
};
/**
* Updates active button with attribute specified in layout
* @Param {object} graphInfo plot object containing data and layout
* @Return {HTMLelement}
*/
proto.updateActiveButton = function(buttonClicked) {
var fullLayout = this.graphInfo._fullLayout,
dataAttrClicked = (buttonClicked !== undefined) ?
buttonClicked.getAttribute('data-attr') :
null;
this.buttonElements.forEach(function(button) {
var thisval = button.getAttribute('data-val') || true,
dataAttr = button.getAttribute('data-attr'),
isToggleButton = (button.getAttribute('data-toggle') === 'true'),
button3 = d3.select(button);
// Use 'data-toggle' and 'buttonClicked' to toggle buttons
// that have no one-to-one equivalent in fullLayout
if(isToggleButton) {
if(dataAttr === dataAttrClicked) {
button3.classed('active', !button3.classed('active'));
}
}
else {
var val = (dataAttr === null) ?
dataAttr :
Lib.nestedProperty(fullLayout, dataAttr).get();
button3.classed('active', val === thisval);
}
});
};
/**
* Check if modeBar is configured as button configuration argument
*
* @Param {object} buttons 2d array of grouped button config objects
* @Return {boolean}
*/
proto.hasButtons = function(buttons) {
var currentButtons = this.buttons;
if(!currentButtons) return false;
if(buttons.length !== currentButtons.length) return false;
for(var i = 0; i < buttons.length; ++i) {
if(buttons[i].length !== currentButtons[i].length) return false;
for(var j = 0; j < buttons[i].length; j++) {
if(buttons[i][j].name !== currentButtons[i][j].name) return false;
}
}
return true;
};
/**
* @return {HTMLDivElement} The logo image wrapped in a group
*/
proto.getLogo = function() {
var group = this.createGroup(),
a = document.createElement('a');
a.href = 'https://plot.ly/';
a.target = '_blank';
a.setAttribute('data-title', 'Produced with Plotly');
a.className = 'modebar-btn plotlyjsicon modebar-btn--logo';
a.appendChild(this.createIcon(Icons.plotlylogo));
group.appendChild(a);
return group;
};
proto.removeAllButtons = function() {
while(this.element.firstChild) {
this.element.removeChild(this.element.firstChild);
}
this.hasLogo = false;
};
proto.destroy = function() {
Lib.removeElement(this.container.querySelector('.modebar'));
};
function createModeBar(gd, buttons) {
var fullLayout = gd._fullLayout;
var modeBar = new ModeBar({
graphInfo: gd,
container: fullLayout._paperdiv.node(),
buttons: buttons
});
if(fullLayout._privateplot) {
d3.select(modeBar.element).append('span')
.classed('badge-private float--left', true)
.text('PRIVATE');
}
return modeBar;
}
module.exports = createModeBar;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| button_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| get_update_object.js | 13.04% | (3 / 23) | 0% | (0 / 4) | 0% | (0 / 2) | 13.04% | (3 / 23) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
step: {
valType: 'enumerated',
role: 'info',
values: ['month', 'year', 'day', 'hour', 'minute', 'second', 'all'],
dflt: 'month',
description: [
'The unit of measurement that the `count` value will set the range by.'
].join(' ')
},
stepmode: {
valType: 'enumerated',
role: 'info',
values: ['backward', 'todate'],
dflt: 'backward',
description: [
'Sets the range update mode.',
'If *backward*, the range update shifts the start of range',
'back *count* times *step* milliseconds.',
'If *todate*, the range update shifts the start of range',
'back to the first timestamp from *count* times',
'*step* milliseconds back.',
'For example, with `step` set to *year* and `count` set to *1*',
'the range update shifts the start of the range back to',
'January 01 of the current year.',
'Month and year *todate* are currently available only',
'for the built-in (Gregorian) calendar.'
].join(' ')
},
count: {
valType: 'number',
role: 'info',
min: 0,
dflt: 1,
description: [
'Sets the number of steps to take to update the range.',
'Use with `step` to specify the update interval.'
].join(' ')
},
label: {
valType: 'string',
role: 'info',
description: 'Sets the text label to appear on the button.'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
module.exports = function getUpdateObject(axisLayout, buttonLayout) {
var axName = axisLayout._name;
var update = {};
if(buttonLayout.step === 'all') {
update[axName + '.autorange'] = true;
}
else {
var xrange = getXRange(axisLayout, buttonLayout);
update[axName + '.range[0]'] = xrange[0];
update[axName + '.range[1]'] = xrange[1];
}
return update;
};
function getXRange(axisLayout, buttonLayout) {
var currentRange = axisLayout.range;
var base = new Date(axisLayout.r2l(currentRange[1]));
var step = buttonLayout.step,
count = buttonLayout.count;
var range0;
switch(buttonLayout.stepmode) {
case 'backward':
range0 = axisLayout.l2r(+d3.time[step].utc.offset(base, -count));
break;
case 'todate':
var base2 = d3.time[step].utc.offset(base, -count);
range0 = axisLayout.l2r(+d3.time[step].utc.ceil(base2));
break;
}
var range1 = currentRange[1];
return [range0, range1];
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 40% | (2 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 40% | (2 / 5) | |
| shape_defaults.js | 9.3% | (4 / 43) | 0% | (0 / 14) | 0% | (0 / 2) | 9.52% | (4 / 42) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var annAttrs = require('../annotations/attributes');
var scatterLineAttrs = require('../../traces/scatter/attributes').line;
var dash = require('../drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
_isLinkedToArray: 'shape',
visible: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not this shape is visible.'
].join(' ')
},
type: {
valType: 'enumerated',
values: ['circle', 'rect', 'path', 'line'],
role: 'info',
description: [
'Specifies the shape type to be drawn.',
'If *line*, a line is drawn from (`x0`,`y0`) to (`x1`,`y1`)',
'If *circle*, a circle is drawn from',
'((`x0`+`x1`)/2, (`y0`+`y1`)/2))',
'with radius',
'(|(`x0`+`x1`)/2 - `x0`|, |(`y0`+`y1`)/2 -`y0`)|)',
'If *rect*, a rectangle is drawn linking',
'(`x0`,`y0`), (`x1`,`y0`), (`x1`,`y1`), (`x0`,`y1`), (`x0`,`y0`)',
'If *path*, draw a custom SVG path using `path`.'
].join(' ')
},
layer: {
valType: 'enumerated',
values: ['below', 'above'],
dflt: 'above',
role: 'info',
description: 'Specifies whether shapes are drawn below or above traces.'
},
xref: extendFlat({}, annAttrs.xref, {
description: [
'Sets the shape\'s x coordinate axis.',
'If set to an x axis id (e.g. *x* or *x2*), the `x` position',
'refers to an x coordinate',
'If set to *paper*, the `x` position refers to the distance from',
'the left side of the plotting area in normalized coordinates',
'where *0* (*1*) corresponds to the left (right) side.',
'If the axis `type` is *log*, then you must take the',
'log of your desired range.',
'If the axis `type` is *date*, then you must convert',
'the date to unix time in milliseconds.'
].join(' ')
}),
x0: {
valType: 'any',
role: 'info',
description: [
'Sets the shape\'s starting x position.',
'See `type` for more info.'
].join(' ')
},
x1: {
valType: 'any',
role: 'info',
description: [
'Sets the shape\'s end x position.',
'See `type` for more info.'
].join(' ')
},
yref: extendFlat({}, annAttrs.yref, {
description: [
'Sets the annotation\'s y coordinate axis.',
'If set to an y axis id (e.g. *y* or *y2*), the `y` position',
'refers to an y coordinate',
'If set to *paper*, the `y` position refers to the distance from',
'the bottom of the plotting area in normalized coordinates',
'where *0* (*1*) corresponds to the bottom (top).'
].join(' ')
}),
y0: {
valType: 'any',
role: 'info',
description: [
'Sets the shape\'s starting y position.',
'See `type` for more info.'
].join(' ')
},
y1: {
valType: 'any',
role: 'info',
description: [
'Sets the shape\'s end y position.',
'See `type` for more info.'
].join(' ')
},
path: {
valType: 'string',
role: 'info',
description: [
'For `type` *path* - a valid SVG path but with the pixel values',
'replaced by data values. There are a few restrictions / quirks',
'only absolute instructions, not relative. So the allowed segments',
'are: M, L, H, V, Q, C, T, S, and Z',
'arcs (A) are not allowed because radius rx and ry are relative.',
'In the future we could consider supporting relative commands,',
'but we would have to decide on how to handle date and log axes.',
'Note that even as is, Q and C Bezier paths that are smooth on',
'linear axes may not be smooth on log, and vice versa.',
'no chained "polybezier" commands - specify the segment type for',
'each one.',
'On category axes, values are numbers scaled to the serial numbers',
'of categories because using the categories themselves there would',
'be no way to describe fractional positions',
'On data axes: because space and T are both normal components of path',
'strings, we can\'t use either to separate date from time parts.',
'Therefore we\'ll use underscore for this purpose:',
'2015-02-21_13:45:56.789'
].join(' ')
},
opacity: {
valType: 'number',
min: 0,
max: 1,
dflt: 1,
role: 'info',
description: 'Sets the opacity of the shape.'
},
line: {
color: scatterLineAttrs.color,
width: scatterLineAttrs.width,
dash: dash,
role: 'info'
},
fillcolor: {
valType: 'color',
dflt: 'rgba(0,0,0,0)',
role: 'info',
description: [
'Sets the color filling the shape\'s interior.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 2 2 2 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var attributes = require('./attributes');
var helpers = require('./helpers');
module.exports = function handleShapeDefaults(shapeIn, shapeOut, fullLayout, opts, itemOpts) {
opts = opts || {};
itemOpts = itemOpts || {};
function coerce(attr, dflt) {
return Lib.coerce(shapeIn, shapeOut, attributes, attr, dflt);
}
var visible = coerce('visible', !itemOpts.itemIsNotPlainObject);
if(!visible) return shapeOut;
coerce('layer');
coerce('opacity');
coerce('fillcolor');
coerce('line.color');
coerce('line.width');
coerce('line.dash');
var dfltType = shapeIn.path ? 'path' : 'rect',
shapeType = coerce('type', dfltType);
// positioning
var axLetters = ['x', 'y'];
for(var i = 0; i < 2; i++) {
var axLetter = axLetters[i],
gdMock = {_fullLayout: fullLayout};
// xref, yref
var axRef = Axes.coerceRef(shapeIn, shapeOut, gdMock, axLetter, '', 'paper');
if(shapeType !== 'path') {
var dflt0 = 0.25,
dflt1 = 0.75,
ax,
pos2r,
r2pos;
if(axRef !== 'paper') {
ax = Axes.getFromId(gdMock, axRef);
r2pos = helpers.rangeToShapePosition(ax);
pos2r = helpers.shapePositionToRange(ax);
}
else {
pos2r = r2pos = Lib.identity;
}
// hack until V2.0 when log has regular range behavior - make it look like other
// ranges to send to coerce, then put it back after
// this is all to give reasonable default position behavior on log axes, which is
// a pretty unimportant edge case so we could just ignore this.
var attr0 = axLetter + '0',
attr1 = axLetter + '1',
in0 = shapeIn[attr0],
in1 = shapeIn[attr1];
shapeIn[attr0] = pos2r(shapeIn[attr0], true);
shapeIn[attr1] = pos2r(shapeIn[attr1], true);
// x0, x1 (and y0, y1)
Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr0, dflt0);
Axes.coercePosition(shapeOut, gdMock, coerce, axRef, attr1, dflt1);
// hack part 2
shapeOut[attr0] = r2pos(shapeOut[attr0]);
shapeOut[attr1] = r2pos(shapeOut[attr1]);
shapeIn[attr0] = in0;
shapeIn[attr1] = in1;
}
}
if(shapeType === 'path') {
coerce('path');
}
else {
Lib.noneOrAll(shapeIn, shapeOut, ['x0', 'x1', 'y0', 'y1']);
}
return shapeOut;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 20.83% | (15 / 72) | 0% | (0 / 50) | 0% | (0 / 11) | 22.06% | (15 / 68) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Plotly = require('../../plotly');
var Plots = require('../../plots/plots');
var Lib = require('../../lib');
var Drawing = require('../drawing');
var Color = require('../color');
var svgTextUtils = require('../../lib/svg_text_utils');
var interactConstants = require('../../constants/interactions');
var Titles = module.exports = {};
/**
* Titles - (re)draw titles on the axes and plot:
* @param {DOM element} gd - the graphDiv
* @param {string} titleClass - the css class of this title
* @param {object} options - how and what to draw
* propContainer - the layout object containing `title` and `titlefont`
* attributes that apply to this title
* propName - the full name of the title property (for Plotly.relayout)
* [traceIndex] - include only if this property applies to one trace
* (such as a colorbar title) - then editing pipes to Plotly.restyle
* instead of Plotly.relayout
* dfltName - the name of the title in placeholder text
* [avoid] {object} - include if this title should move to avoid other elements
* selection - d3 selection of elements to avoid
* side - which direction to move if there is a conflict
* [offsetLeft] - if these elements are subject to a translation
* wrt the title element
* [offsetTop]
* attributes {object} - position and alignment attributes
* x - pixels
* y - pixels
* text-anchor - start|middle|end
* transform {object} - how to transform the title after positioning
* rotate - degrees
* offset - shift up/down in the rotated frame (unused?)
* containerGroup - if an svg <g> element already exists to hold this
* title, include here. Otherwise it will go in fullLayout._infolayer
*/
Titles.draw = function(gd, titleClass, options) {
var cont = options.propContainer,
prop = options.propName,
traceIndex = options.traceIndex,
name = options.dfltName,
avoid = options.avoid || {},
attributes = options.attributes,
transform = options.transform,
group = options.containerGroup,
fullLayout = gd._fullLayout,
font = cont.titlefont.family,
fontSize = cont.titlefont.size,
fontColor = cont.titlefont.color,
opacity = 1,
isplaceholder = false,
txt = cont.title.trim();
if(txt === '') opacity = 0;
if(txt.match(/Click to enter .+ title/)) {
opacity = 0.2;
isplaceholder = true;
}
if(!group) {
group = fullLayout._infolayer.selectAll('.g-' + titleClass)
.data([0]);
group.enter().append('g')
.classed('g-' + titleClass, true);
}
var el = group.selectAll('text')
.data([0]);
el.enter().append('text');
el.text(txt)
// this is hacky, but convertToTspans uses the class
// to determine whether to rotate mathJax...
// so we need to clear out any old class and put the
// correct one (only relevant for colorbars, at least
// for now) - ie don't use .classed
.attr('class', titleClass);
function titleLayout(titleEl) {
Lib.syncOrAsync([drawTitle, scootTitle], titleEl);
}
function drawTitle(titleEl) {
titleEl.attr('transform', transform ?
'rotate(' + [transform.rotate, attributes.x, attributes.y] +
') translate(0, ' + transform.offset + ')' :
null);
titleEl.style({
'font-family': font,
'font-size': d3.round(fontSize, 2) + 'px',
fill: Color.rgb(fontColor),
opacity: opacity * Color.opacity(fontColor),
'font-weight': Plots.fontWeight
})
.attr(attributes)
.call(svgTextUtils.convertToTspans)
.attr(attributes);
titleEl.selectAll('tspan.line')
.attr(attributes);
return Plots.previousPromises(gd);
}
function scootTitle(titleElIn) {
var titleGroup = d3.select(titleElIn.node().parentNode);
if(avoid && avoid.selection && avoid.side && txt) {
titleGroup.attr('transform', null);
// move toward avoid.side (= left, right, top, bottom) if needed
// can include pad (pixels, default 2)
var shift = 0,
backside = {
left: 'right',
right: 'left',
top: 'bottom',
bottom: 'top'
}[avoid.side],
shiftSign = (['left', 'top'].indexOf(avoid.side) !== -1) ?
-1 : 1,
pad = isNumeric(avoid.pad) ? avoid.pad : 2,
titlebb = Drawing.bBox(titleGroup.node()),
paperbb = {
left: 0,
top: 0,
right: fullLayout.width,
bottom: fullLayout.height
},
maxshift = avoid.maxShift || (
(paperbb[avoid.side] - titlebb[avoid.side]) *
((avoid.side === 'left' || avoid.side === 'top') ? -1 : 1));
// Prevent the title going off the paper
if(maxshift < 0) shift = maxshift;
else {
// so we don't have to offset each avoided element,
// give the title the opposite offset
var offsetLeft = avoid.offsetLeft || 0,
offsetTop = avoid.offsetTop || 0;
titlebb.left -= offsetLeft;
titlebb.right -= offsetLeft;
titlebb.top -= offsetTop;
titlebb.bottom -= offsetTop;
// iterate over a set of elements (avoid.selection)
// to avoid collisions with
avoid.selection.each(function() {
var avoidbb = Drawing.bBox(this);
if(Lib.bBoxIntersect(titlebb, avoidbb, pad)) {
shift = Math.max(shift, shiftSign * (
avoidbb[avoid.side] - titlebb[backside]) + pad);
}
});
shift = Math.min(maxshift, shift);
}
if(shift > 0 || maxshift < 0) {
var shiftTemplate = {
left: [-shift, 0],
right: [shift, 0],
top: [0, -shift],
bottom: [0, shift]
}[avoid.side];
titleGroup.attr('transform',
'translate(' + shiftTemplate + ')');
}
}
}
el.attr({'data-unformatted': txt})
.call(titleLayout);
var placeholderText = 'Click to enter ' + name + ' title';
function setPlaceholder() {
opacity = 0;
isplaceholder = true;
txt = placeholderText;
el.attr({'data-unformatted': txt})
.text(txt)
.on('mouseover.opacity', function() {
d3.select(this).transition()
.duration(interactConstants.SHOW_PLACEHOLDER).style('opacity', 1);
})
.on('mouseout.opacity', function() {
d3.select(this).transition()
.duration(interactConstants.HIDE_PLACEHOLDER).style('opacity', 0);
});
}
if(gd._context.editable) {
if(!txt) setPlaceholder();
else el.on('.opacity', null);
el.call(svgTextUtils.makeEditable)
.on('edit', function(text) {
if(traceIndex !== undefined) Plotly.restyle(gd, prop, text, traceIndex);
else Plotly.relayout(gd, prop, text);
})
.on('cancel', function() {
this.text(this.attr('data-unformatted'))
.call(titleLayout);
})
.on('input', function(d) {
this.text(d || ' ').attr(attributes)
.selectAll('tspan.line')
.attr(attributes);
});
}
else if(!txt || txt.match(/Click to enter .+ title/)) {
el.remove();
}
el.classed('js-placeholder', isplaceholder);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| scrollbox.js | 10.9% | (17 / 156) | 0% | (0 / 76) | 0% | (0 / 9) | 11.04% | (17 / 154) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = ScrollBox;
var d3 = require('d3');
var Color = require('../color');
var Drawing = require('../drawing');
var Lib = require('../../lib');
/**
* Helper class to setup a scroll box
*
* @class
* @param gd Plotly's graph div
* @param container Container to be scroll-boxed (as a D3 selection)
* @param {string} id Id for the clip path to implement the scroll box
*/
function ScrollBox(gd, container, id) {
this.gd = gd;
this.container = container;
this.id = id;
// See ScrollBox.prototype.enable for further definition
this.position = null; // scrollbox position
this.translateX = null; // scrollbox horizontal translation
this.translateY = null; // scrollbox vertical translation
this.hbar = null; // horizontal scrollbar D3 selection
this.vbar = null; // vertical scrollbar D3 selection
// <rect> element to capture pointer events
this.bg = this.container.selectAll('rect.scrollbox-bg').data([0]);
this.bg.exit()
.on('.drag', null)
.on('wheel', null)
.remove();
this.bg.enter().append('rect')
.classed('scrollbox-bg', true)
.style('pointer-events', 'all')
.attr({
opacity: 0,
x: 0,
y: 0,
width: 0,
height: 0
});
}
// scroll bar dimensions
ScrollBox.barWidth = 2;
ScrollBox.barLength = 20;
ScrollBox.barRadius = 2;
ScrollBox.barPad = 1;
ScrollBox.barColor = '#808BA4';
/**
* If needed, setup a clip path and scrollbars
*
* @method
* @param {Object} position
* @param {number} position.l Left side position (in pixels)
* @param {number} position.t Top side (in pixels)
* @param {number} position.w Width (in pixels)
* @param {number} position.h Height (in pixels)
* @param {string} [position.direction='down']
* Either 'down', 'left', 'right' or 'up'
* @param {number} [translateX=0] Horizontal offset (in pixels)
* @param {number} [translateY=0] Vertical offset (in pixels)
*/
ScrollBox.prototype.enable = function enable(position, translateX, translateY) {
var fullLayout = this.gd._fullLayout,
fullWidth = fullLayout.width,
fullHeight = fullLayout.height;
// compute position of scrollbox
this.position = position;
var l = this.position.l,
w = this.position.w,
t = this.position.t,
h = this.position.h,
direction = this.position.direction,
isDown = (direction === 'down'),
isLeft = (direction === 'left'),
isRight = (direction === 'right'),
isUp = (direction === 'up'),
boxW = w,
boxH = h,
boxL, boxR,
boxT, boxB;
if(!isDown && !isLeft && !isRight && !isUp) {
this.position.direction = 'down';
isDown = true;
}
var isVertical = isDown || isUp;
if(isVertical) {
boxL = l;
boxR = boxL + boxW;
if(isDown) {
// anchor to top side
boxT = t;
boxB = Math.min(boxT + boxH, fullHeight);
boxH = boxB - boxT;
}
else {
// anchor to bottom side
boxB = t + boxH;
boxT = Math.max(boxB - boxH, 0);
boxH = boxB - boxT;
}
}
else {
boxT = t;
boxB = boxT + boxH;
if(isLeft) {
// anchor to right side
boxR = l + boxW;
boxL = Math.max(boxR - boxW, 0);
boxW = boxR - boxL;
}
else {
// anchor to left side
boxL = l;
boxR = Math.min(boxL + boxW, fullWidth);
boxW = boxR - boxL;
}
}
this._box = {
l: boxL,
t: boxT,
w: boxW,
h: boxH
};
// compute position of horizontal scroll bar
var needsHorizontalScrollBar = (w > boxW),
hbarW = ScrollBox.barLength + 2 * ScrollBox.barPad,
hbarH = ScrollBox.barWidth + 2 * ScrollBox.barPad,
// draw horizontal scrollbar on the bottom side
hbarL = l,
hbarT = t + h;
if(hbarT + hbarH > fullHeight) hbarT = fullHeight - hbarH;
var hbar = this.container.selectAll('rect.scrollbar-horizontal').data(
(needsHorizontalScrollBar) ? [0] : []);
hbar.exit()
.on('.drag', null)
.remove();
hbar.enter().append('rect')
.classed('scrollbar-horizontal', true)
.call(Color.fill, ScrollBox.barColor);
if(needsHorizontalScrollBar) {
this.hbar = hbar.attr({
'rx': ScrollBox.barRadius,
'ry': ScrollBox.barRadius,
'x': hbarL,
'y': hbarT,
'width': hbarW,
'height': hbarH
});
// hbar center moves between hbarXMin and hbarXMin + hbarTranslateMax
this._hbarXMin = hbarL + hbarW / 2;
this._hbarTranslateMax = boxW - hbarW;
}
else {
delete this.hbar;
delete this._hbarXMin;
delete this._hbarTranslateMax;
}
// compute position of vertical scroll bar
var needsVerticalScrollBar = (h > boxH),
vbarW = ScrollBox.barWidth + 2 * ScrollBox.barPad,
vbarH = ScrollBox.barLength + 2 * ScrollBox.barPad,
// draw vertical scrollbar on the right side
vbarL = l + w,
vbarT = t;
if(vbarL + vbarW > fullWidth) vbarL = fullWidth - vbarW;
var vbar = this.container.selectAll('rect.scrollbar-vertical').data(
(needsVerticalScrollBar) ? [0] : []);
vbar.exit()
.on('.drag', null)
.remove();
vbar.enter().append('rect')
.classed('scrollbar-vertical', true)
.call(Color.fill, ScrollBox.barColor);
if(needsVerticalScrollBar) {
this.vbar = vbar.attr({
'rx': ScrollBox.barRadius,
'ry': ScrollBox.barRadius,
'x': vbarL,
'y': vbarT,
'width': vbarW,
'height': vbarH
});
// vbar center moves between vbarYMin and vbarYMin + vbarTranslateMax
this._vbarYMin = vbarT + vbarH / 2;
this._vbarTranslateMax = boxH - vbarH;
}
else {
delete this.vbar;
delete this._vbarYMin;
delete this._vbarTranslateMax;
}
// setup a clip path (if scroll bars are needed)
var clipId = this.id,
clipL = boxL - 0.5,
clipR = (needsVerticalScrollBar) ? boxR + vbarW + 0.5 : boxR + 0.5,
clipT = boxT - 0.5,
clipB = (needsHorizontalScrollBar) ? boxB + hbarH + 0.5 : boxB + 0.5;
var clipPath = fullLayout._topdefs.selectAll('#' + clipId)
.data((needsHorizontalScrollBar || needsVerticalScrollBar) ? [0] : []);
clipPath.exit().remove();
clipPath.enter()
.append('clipPath').attr('id', clipId)
.append('rect');
if(needsHorizontalScrollBar || needsVerticalScrollBar) {
this._clipRect = clipPath.select('rect').attr({
x: Math.floor(clipL),
y: Math.floor(clipT),
width: Math.ceil(clipR) - Math.floor(clipL),
height: Math.ceil(clipB) - Math.floor(clipT)
});
this.container.call(Drawing.setClipUrl, clipId);
this.bg.attr({
x: l,
y: t,
width: w,
height: h
});
}
else {
this.bg.attr({
width: 0,
height: 0
});
this.container
.on('wheel', null)
.on('.drag', null)
.call(Drawing.setClipUrl, null);
delete this._clipRect;
}
// set up drag listeners (if scroll bars are needed)
if(needsHorizontalScrollBar || needsVerticalScrollBar) {
var onBoxDrag = d3.behavior.drag()
.on('dragstart', function() {
d3.event.sourceEvent.preventDefault();
})
.on('drag', this._onBoxDrag.bind(this));
this.container
.on('wheel', null)
.on('wheel', this._onBoxWheel.bind(this))
.on('.drag', null)
.call(onBoxDrag);
var onBarDrag = d3.behavior.drag()
.on('dragstart', function() {
d3.event.sourceEvent.preventDefault();
d3.event.sourceEvent.stopPropagation();
})
.on('drag', this._onBarDrag.bind(this));
if(needsHorizontalScrollBar) {
this.hbar
.on('.drag', null)
.call(onBarDrag);
}
if(needsVerticalScrollBar) {
this.vbar
.on('.drag', null)
.call(onBarDrag);
}
}
// set scrollbox translation
this.setTranslate(translateX, translateY);
};
/**
* If present, remove clip-path and scrollbars
*
* @method
*/
ScrollBox.prototype.disable = function disable() {
if(this.hbar || this.vbar) {
this.bg.attr({
width: 0,
height: 0
});
this.container
.on('wheel', null)
.on('.drag', null)
.call(Drawing.setClipUrl, null);
delete this._clipRect;
}
if(this.hbar) {
this.hbar.on('.drag', null);
this.hbar.remove();
delete this.hbar;
delete this._hbarXMin;
delete this._hbarTranslateMax;
}
if(this.vbar) {
this.vbar.on('.drag', null);
this.vbar.remove();
delete this.vbar;
delete this._vbarYMin;
delete this._vbarTranslateMax;
}
};
/**
* Handles scroll box drag events
*
* @method
*/
ScrollBox.prototype._onBoxDrag = function onBarDrag() {
var translateX = this.translateX,
translateY = this.translateY;
if(this.hbar) {
translateX -= d3.event.dx;
}
if(this.vbar) {
translateY -= d3.event.dy;
}
this.setTranslate(translateX, translateY);
};
/**
* Handles scroll box wheel events
*
* @method
*/
ScrollBox.prototype._onBoxWheel = function onBarWheel() {
var translateX = this.translateX,
translateY = this.translateY;
if(this.hbar) {
translateX += d3.event.deltaY;
}
if(this.vbar) {
translateY += d3.event.deltaY;
}
this.setTranslate(translateX, translateY);
};
/**
* Handles scroll bar drag events
*
* @method
*/
ScrollBox.prototype._onBarDrag = function onBarDrag() {
var translateX = this.translateX,
translateY = this.translateY;
if(this.hbar) {
var xMin = translateX + this._hbarXMin,
xMax = xMin + this._hbarTranslateMax,
x = Lib.constrain(d3.event.x, xMin, xMax),
xf = (x - xMin) / (xMax - xMin);
var translateXMax = this.position.w - this._box.w;
translateX = xf * translateXMax;
}
if(this.vbar) {
var yMin = translateY + this._vbarYMin,
yMax = yMin + this._vbarTranslateMax,
y = Lib.constrain(d3.event.y, yMin, yMax),
yf = (y - yMin) / (yMax - yMin);
var translateYMax = this.position.h - this._box.h;
translateY = yf * translateYMax;
}
this.setTranslate(translateX, translateY);
};
/**
* Set clip path and scroll bar translate transform
*
* @method
* @param {number} [translateX=0] Horizontal offset (in pixels)
* @param {number} [translateY=0] Vertical offset (in pixels)
*/
ScrollBox.prototype.setTranslate = function setTranslate(translateX, translateY) {
// store translateX and translateY (needed by mouse event handlers)
var translateXMax = this.position.w - this._box.w,
translateYMax = this.position.h - this._box.h;
translateX = Lib.constrain(translateX || 0, 0, translateXMax);
translateY = Lib.constrain(translateY || 0, 0, translateYMax);
this.translateX = translateX;
this.translateY = translateY;
this.container.call(Drawing.setTranslate,
this._box.l - this.position.l - translateX,
this._box.t - this.position.t - translateY);
if(this._clipRect) {
this._clipRect.attr({
x: Math.floor(this.position.l + translateX - 0.5),
y: Math.floor(this.position.t + translateY - 0.5)
});
}
if(this.hbar) {
var xf = translateX / translateXMax;
this.hbar.call(Drawing.setTranslate,
translateX + xf * this._hbarTranslateMax,
translateY);
}
if(this.vbar) {
var yf = translateY / translateYMax;
this.vbar.call(Drawing.setTranslate,
translateX,
translateY + yf * this._vbarTranslateMax);
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| gl2d_dashes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| gl3d_dashes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| gl_markers.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| interactions.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| numerical.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| string_mappings.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| xmlns_namespaces.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (4 / 4) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
solid: [1],
dot: [1, 1],
dash: [4, 1],
longdash: [8, 1],
dashdot: [4, 1, 1, 1],
longdashdot: [8, 1, 1, 1]
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
solid: [[], 0],
dot: [[0.5, 1], 200],
dash: [[0.5, 1], 50],
longdash: [[0.5, 1], 10],
dashdot: [[0.5, 0.625, 0.875, 1], 50],
longdashdot: [[0.5, 0.7, 0.8, 1], 10]
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
circle: '●',
'circle-open': '○',
square: '■',
'square-open': '□',
diamond: '◆',
'diamond-open': '◇',
cross: '+',
x: '❌'
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
/**
* Timing information for interactive elements
*/
SHOW_PLACEHOLDER: 100,
HIDE_PLACEHOLDER: 1000,
// ms between first mousedown and 2nd mouseup to constitute dblclick...
// we don't seem to have access to the system setting
DBLCLICKDELAY: 300
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
/**
* Standardize all missing data in calcdata to use undefined
* never null or NaN.
* That way we can use !==undefined, or !== BADNUM,
* to test for real data
*/
BADNUM: undefined,
/*
* Limit certain operations to well below floating point max value
* to avoid glitches: Make sure that even when you multiply it by the
* number of pixels on a giant screen it still works
*/
FP_SAFE: Number.MAX_VALUE / 10000,
/*
* conversion of date units to milliseconds
* year and month constants are marked "AVG"
* to remind us that not all years and months
* have the same length
*/
ONEAVGYEAR: 31557600000, // 365.25 days
ONEAVGMONTH: 2629800000, // 1/12 of ONEAVGYEAR
ONEDAY: 86400000,
ONEHOUR: 3600000,
ONEMIN: 60000,
ONESEC: 1000,
/*
* For fast conversion btwn world calendars and epoch ms, the Julian Day Number
* of the unix epoch. From calendars.instance().newDate(1970, 1, 1).toJD()
*/
EPOCHJD: 2440587.5,
/*
* Are two values nearly equal? Compare to 1PPM
*/
ALMOST_EQUAL: 1 - 1e-6
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// N.B. HTML entities are listed without the leading '&' and trailing ';'
module.exports = {
entityToUnicode: {
'mu': 'μ',
'amp': '&',
'lt': '<',
'gt': '>',
'nbsp': ' ',
'times': '×',
'plusmn': '±',
'deg': '°'
},
unicodeToEntity: {
'&': 'amp',
'<': 'lt',
'>': 'gt',
'"': 'quot',
'\'': '#x27',
'\/': '#x2F'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
exports.xmlns = 'http://www.w3.org/2000/xmlns/';
exports.svg = 'http://www.w3.org/2000/svg';
exports.xlink = 'http://www.w3.org/1999/xlink';
// the 'old' d3 quirk got fix in v3.5.7
// https://github.com/mbostock/d3/commit/a6f66e9dd37f764403fc7c1f26be09ab4af24fed
exports.svgAttrs = {
xmlns: exports.svg,
'xmlns:xlink': exports.xlink
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| mathjax_config.js | 40% | (2 / 5) | 50% | (1 / 2) | 100% | (0 / 0) | 40% | (2 / 5) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* global MathJax:false */ /** * Check and configure MathJax */ Iif(typeof MathJax !== 'undefined') { exports.MathJax = true; MathJax.Hub.Config({ messageStyle: 'none', skipStartupTypeset: true, displayAlign: 'left', tex2jax: { inlineMath: [['$', '$'], ['\\(', '\\)']] } }); MathJax.Hub.Configured(); } else { exports.MathJax = false; } |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| array_to_calc_item.js | 33.33% | (1 / 3) | 0% | (0 / 2) | 0% | (0 / 1) | 50% | (1 / 2) | |
| clean_number.js | 44.44% | (4 / 9) | 0% | (0 / 4) | 0% | (0 / 1) | 50% | (4 / 8) | |
| coerce.js | 9.02% | (11 / 122) | 0% | (0 / 102) | 0% | (0 / 20) | 10.78% | (11 / 102) | |
| dates.js | 27.35% | (67 / 245) | 14.12% | (24 / 170) | 14.29% | (3 / 21) | 30.73% | (67 / 218) | |
| ensure_array.js | 20% | (1 / 5) | 0% | (0 / 2) | 0% | (0 / 1) | 25% | (1 / 4) | |
| events.js | 5.66% | (3 / 53) | 0% | (0 / 14) | 0% | (0 / 4) | 6.12% | (3 / 49) | |
| extend.js | 53.49% | (23 / 43) | 28.95% | (11 / 38) | 50% | (3 / 6) | 53.49% | (23 / 43) | |
| filter_unique.js | 12.5% | (1 / 8) | 0% | (0 / 2) | 0% | (0 / 1) | 12.5% | (1 / 8) | |
| filter_visible.js | 14.29% | (1 / 7) | 0% | (0 / 2) | 0% | (0 / 1) | 16.67% | (1 / 6) | |
| geo_location_utils.js | 30.43% | (7 / 23) | 0% | (0 / 6) | 0% | (0 / 3) | 33.33% | (7 / 21) | |
| geojson_utils.js | 13.16% | (5 / 38) | 0% | (0 / 16) | 0% | (0 / 4) | 13.89% | (5 / 36) | |
| gl_format_color.js | 28.21% | (11 / 39) | 0% | (0 / 20) | 0% | (0 / 6) | 28.95% | (11 / 38) | |
| html2unicode.js | 26.67% | (8 / 30) | 0% | (0 / 6) | 0% | (0 / 5) | 27.59% | (8 / 29) | |
| identity.js | 50% | (1 / 2) | 100% | (0 / 0) | 0% | (0 / 1) | 100% | (1 / 1) | |
| index.js | 32.21% | (96 / 298) | 0.68% | (1 / 146) | 3.85% | (1 / 26) | 35.69% | (96 / 269) | |
| is_array.js | 50% | (2 / 4) | 50% | (3 / 6) | 0% | (0 / 2) | 50% | (2 / 4) | |
| is_plain_object.js | 50% | (2 / 4) | 14.29% | (1 / 7) | 100% | (1 / 1) | 50% | (2 / 4) | |
| loggers.js | 24% | (6 / 25) | 0% | (0 / 12) | 0% | (0 / 4) | 24% | (6 / 25) | |
| matrix.js | 16% | (8 / 50) | 0% | (0 / 14) | 0% | (0 / 10) | 18.6% | (8 / 43) | |
| mod.js | 33.33% | (1 / 3) | 0% | (0 / 2) | 0% | (0 / 1) | 33.33% | (1 / 3) | |
| nested_property.js | 10.96% | (16 / 146) | 0% | (0 / 105) | 0% | (0 / 14) | 12.7% | (16 / 126) | |
| noop.js | 100% | (1 / 1) | 100% | (0 / 0) | 0% | (0 / 1) | 100% | (1 / 1) | |
| notifier.js | 15.63% | (5 / 32) | 0% | (0 / 10) | 0% | (0 / 5) | 18.52% | (5 / 27) | |
| override_cursor.js | 20% | (4 / 20) | 0% | (0 / 14) | 0% | (0 / 1) | 21.05% | (4 / 19) | |
| polygon.js | 9.88% | (8 / 81) | 0% | (0 / 68) | 0% | (0 / 8) | 10.81% | (8 / 74) | |
| push_unique.js | 10% | (1 / 10) | 0% | (0 / 10) | 0% | (0 / 1) | 11.11% | (1 / 9) | |
| queue.js | 14.29% | (11 / 77) | 0% | (0 / 42) | 0% | (0 / 7) | 14.67% | (11 / 75) | |
| relink_private.js | 16.67% | (3 / 18) | 0% | (0 / 23) | 0% | (0 / 1) | 18.75% | (3 / 16) | |
| search.js | 23.91% | (11 / 46) | 0% | (0 / 32) | 0% | (0 / 9) | 29.73% | (11 / 37) | |
| setcursor.js | 16.67% | (1 / 6) | 0% | (0 / 6) | 0% | (0 / 2) | 25% | (1 / 4) | |
| show_no_webgl_msg.js | 16.67% | (3 / 18) | 0% | (0 / 2) | 0% | (0 / 4) | 17.65% | (3 / 17) | |
| stats.js | 15.56% | (7 / 45) | 0% | (0 / 22) | 0% | (0 / 9) | 21.88% | (7 / 32) | |
| str2rgbarray.js | 60% | (3 / 5) | 0% | (0 / 2) | 0% | (0 / 1) | 60% | (3 / 5) | |
| svg_text_utils.js | 11.24% | (30 / 267) | 0% | (0 / 148) | 5% | (2 / 40) | 12.55% | (30 / 239) | |
| to_log_range.js | 25% | (2 / 8) | 0% | (0 / 4) | 0% | (0 / 1) | 33.33% | (2 / 6) | |
| topojson_utils.js | 60% | (6 / 10) | 100% | (0 / 0) | 0% | (0 / 3) | 60% | (6 / 10) | |
| typed_array_truncate.js | 18.75% | (3 / 16) | 0% | (0 / 4) | 0% | (0 / 3) | 25% | (3 / 12) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // similar to Lib.mergeArray, but using inside a loop module.exports = function arrayToCalcItem(traceAttr, calcItem, calcAttr, i) { if(Array.isArray(traceAttr)) calcItem[calcAttr] = traceAttr[i]; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var BADNUM = require('../constants/numerical').BADNUM;
// precompile for speed
var JUNK = /^['"%,$#\s']+|[, ]|['"%,$#\s']+$/g;
/**
* cleanNumber: remove common leading and trailing cruft
* Always returns either a number or BADNUM.
*/
module.exports = function cleanNumber(v) {
if(typeof v === 'string') {
v = v.replace(JUNK, '');
}
if(isNumeric(v)) return Number(v);
return BADNUM;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var tinycolor = require('tinycolor2');
var getColorscale = require('../components/colorscale/get_scale');
var colorscaleNames = Object.keys(require('../components/colorscale/scales'));
var nestedProperty = require('./nested_property');
var ID_REGEX = /^([2-9]|[1-9][0-9]+)$/;
exports.valObjects = {
data_array: {
// You can use *dflt=[] to force said array to exist though.
description: [
'An {array} of data.',
'The value MUST be an {array}, or we ignore it.'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt'],
coerceFunction: function(v, propOut, dflt) {
if(Array.isArray(v)) propOut.set(v);
else if(dflt !== undefined) propOut.set(dflt);
}
},
enumerated: {
description: [
'Enumerated value type. The available values are listed',
'in `values`.'
].join(' '),
requiredOpts: ['values'],
otherOpts: ['dflt', 'coerceNumber', 'arrayOk'],
coerceFunction: function(v, propOut, dflt, opts) {
if(opts.coerceNumber) v = +v;
if(opts.values.indexOf(v) === -1) propOut.set(dflt);
else propOut.set(v);
}
},
'boolean': {
description: 'A boolean (true/false) value.',
requiredOpts: [],
otherOpts: ['dflt'],
coerceFunction: function(v, propOut, dflt) {
if(v === true || v === false) propOut.set(v);
else propOut.set(dflt);
}
},
number: {
description: [
'A number or a numeric value',
'(e.g. a number inside a string).',
'When applicable, values greater (less) than `max` (`min`)',
'are coerced to the `dflt`.'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt', 'min', 'max', 'arrayOk'],
coerceFunction: function(v, propOut, dflt, opts) {
if(!isNumeric(v) ||
(opts.min !== undefined && v < opts.min) ||
(opts.max !== undefined && v > opts.max)) {
propOut.set(dflt);
}
else propOut.set(+v);
}
},
integer: {
description: [
'An integer or an integer inside a string.',
'When applicable, values greater (less) than `max` (`min`)',
'are coerced to the `dflt`.'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt', 'min', 'max'],
coerceFunction: function(v, propOut, dflt, opts) {
if(v % 1 || !isNumeric(v) ||
(opts.min !== undefined && v < opts.min) ||
(opts.max !== undefined && v > opts.max)) {
propOut.set(dflt);
}
else propOut.set(+v);
}
},
string: {
description: [
'A string value.',
'Numbers are converted to strings except for attributes with',
'`strict` set to true.'
].join(' '),
requiredOpts: [],
// TODO 'values shouldn't be in there (edge case: 'dash' in Scatter)
otherOpts: ['dflt', 'noBlank', 'strict', 'arrayOk', 'values'],
coerceFunction: function(v, propOut, dflt, opts) {
if(typeof v !== 'string') {
var okToCoerce = (typeof v === 'number');
if(opts.strict === true || !okToCoerce) propOut.set(dflt);
else propOut.set(String(v));
}
else if(opts.noBlank && !v) propOut.set(dflt);
else propOut.set(v);
}
},
color: {
description: [
'A string describing color.',
'Supported formats:',
'- hex (e.g. \'#d3d3d3\')',
'- rgb (e.g. \'rgb(255, 0, 0)\')',
'- rgba (e.g. \'rgb(255, 0, 0, 0.5)\')',
'- hsl (e.g. \'hsl(0, 100%, 50%)\')',
'- hsv (e.g. \'hsv(0, 100%, 100%)\')',
'- named colors (full list: http://www.w3.org/TR/css3-color/#svg-color)'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt', 'arrayOk'],
coerceFunction: function(v, propOut, dflt) {
if(tinycolor(v).isValid()) propOut.set(v);
else propOut.set(dflt);
}
},
colorscale: {
description: [
'A Plotly colorscale either picked by a name:',
'(any of', colorscaleNames.join(', '), ')',
'customized as an {array} of 2-element {arrays} where',
'the first element is the normalized color level value',
'(starting at *0* and ending at *1*),',
'and the second item is a valid color string.'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt'],
coerceFunction: function(v, propOut, dflt) {
propOut.set(getColorscale(v, dflt));
}
},
angle: {
description: [
'A number (in degree) between -180 and 180.'
].join(' '),
requiredOpts: [],
otherOpts: ['dflt'],
coerceFunction: function(v, propOut, dflt) {
if(v === 'auto') propOut.set('auto');
else if(!isNumeric(v)) propOut.set(dflt);
else {
if(Math.abs(v) > 180) v -= Math.round(v / 360) * 360;
propOut.set(+v);
}
}
},
subplotid: {
description: [
'An id string of a subplot type (given by dflt), optionally',
'followed by an integer >1. e.g. if dflt=\'geo\', we can have',
'\'geo\', \'geo2\', \'geo3\', ...'
].join(' '),
requiredOpts: ['dflt'],
otherOpts: [],
coerceFunction: function(v, propOut, dflt) {
var dlen = dflt.length;
if(typeof v === 'string' && v.substr(0, dlen) === dflt &&
ID_REGEX.test(v.substr(dlen))) {
propOut.set(v);
return;
}
propOut.set(dflt);
},
validateFunction: function(v, opts) {
var dflt = opts.dflt,
dlen = dflt.length;
if(v === dflt) return true;
if(typeof v !== 'string') return false;
if(v.substr(0, dlen) === dflt && ID_REGEX.test(v.substr(dlen))) {
return true;
}
return false;
}
},
flaglist: {
description: [
'A string representing a combination of flags',
'(order does not matter here).',
'Combine any of the available `flags` with *+*.',
'(e.g. (\'lines+markers\')).',
'Values in `extras` cannot be combined.'
].join(' '),
requiredOpts: ['flags'],
otherOpts: ['dflt', 'extras'],
coerceFunction: function(v, propOut, dflt, opts) {
if(typeof v !== 'string') {
propOut.set(dflt);
return;
}
if((opts.extras || []).indexOf(v) !== -1) {
propOut.set(v);
return;
}
var vParts = v.split('+'),
i = 0;
while(i < vParts.length) {
var vi = vParts[i];
if(opts.flags.indexOf(vi) === -1 || vParts.indexOf(vi) < i) {
vParts.splice(i, 1);
}
else i++;
}
if(!vParts.length) propOut.set(dflt);
else propOut.set(vParts.join('+'));
}
},
any: {
description: 'Any type.',
requiredOpts: [],
otherOpts: ['dflt', 'values', 'arrayOk'],
coerceFunction: function(v, propOut, dflt) {
if(v === undefined) propOut.set(dflt);
else propOut.set(v);
}
},
info_array: {
description: [
'An {array} of plot information.'
].join(' '),
requiredOpts: ['items'],
otherOpts: ['dflt', 'freeLength'],
coerceFunction: function(v, propOut, dflt, opts) {
if(!Array.isArray(v)) {
propOut.set(dflt);
return;
}
var items = opts.items,
vOut = [];
dflt = Array.isArray(dflt) ? dflt : [];
for(var i = 0; i < items.length; i++) {
exports.coerce(v, vOut, items, '[' + i + ']', dflt[i]);
}
propOut.set(vOut);
},
validateFunction: function(v, opts) {
if(!Array.isArray(v)) return false;
var items = opts.items;
// when free length is off, input and declared lengths must match
if(!opts.freeLength && v.length !== items.length) return false;
// valid when all input items are valid
for(var i = 0; i < v.length; i++) {
var isItemValid = exports.validate(v[i], opts.items[i]);
if(!isItemValid) return false;
}
return true;
}
}
};
/**
* Ensures that container[attribute] has a valid value.
*
* attributes[attribute] is an object with possible keys:
* - valType: data_array, enumerated, boolean, ... as in valObjects
* - values: (enumerated only) array of allowed vals
* - min, max: (number, integer only) inclusive bounds on allowed vals
* either or both may be omitted
* - dflt: if attribute is invalid or missing, use this default
* if dflt is provided as an argument to lib.coerce it takes precedence
* as a convenience, returns the value it finally set
*/
exports.coerce = function(containerIn, containerOut, attributes, attribute, dflt) {
var opts = nestedProperty(attributes, attribute).get(),
propIn = nestedProperty(containerIn, attribute),
propOut = nestedProperty(containerOut, attribute),
v = propIn.get();
if(dflt === undefined) dflt = opts.dflt;
/**
* arrayOk: value MAY be an array, then we do no value checking
* at this point, because it can be more complicated than the
* individual form (eg. some array vals can be numbers, even if the
* single values must be color strings)
*/
if(opts.arrayOk && Array.isArray(v)) {
propOut.set(v);
return v;
}
exports.valObjects[opts.valType].coerceFunction(v, propOut, dflt, opts);
return propOut.get();
};
/**
* Variation on coerce
*
* Uses coerce to get attribute value if user input is valid,
* returns attribute default if user input it not valid or
* returns false if there is no user input.
*/
exports.coerce2 = function(containerIn, containerOut, attributes, attribute, dflt) {
var propIn = nestedProperty(containerIn, attribute),
propOut = exports.coerce(containerIn, containerOut, attributes, attribute, dflt),
valIn = propIn.get();
return (valIn !== undefined && valIn !== null) ? propOut : false;
};
/*
* Shortcut to coerce the three font attributes
*
* 'coerce' is a lib.coerce wrapper with implied first three arguments
*/
exports.coerceFont = function(coerce, attr, dfltObj) {
var out = {};
dfltObj = dfltObj || {};
out.family = coerce(attr + '.family', dfltObj.family);
out.size = coerce(attr + '.size', dfltObj.size);
out.color = coerce(attr + '.color', dfltObj.color);
return out;
};
exports.validate = function(value, opts) {
var valObject = exports.valObjects[opts.valType];
if(opts.arrayOk && Array.isArray(value)) return true;
if(valObject.validateFunction) {
return valObject.validateFunction(value, opts);
}
var failed = {},
out = failed,
propMock = { set: function(v) { out = v; } };
// 'failed' just something mutable that won't be === anything else
valObject.coerceFunction(value, propMock, failed, opts);
return out !== failed;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var logError = require('./loggers').error;
var mod = require('./mod');
var constants = require('../constants/numerical');
var BADNUM = constants.BADNUM;
var ONEDAY = constants.ONEDAY;
var ONEHOUR = constants.ONEHOUR;
var ONEMIN = constants.ONEMIN;
var ONESEC = constants.ONESEC;
var EPOCHJD = constants.EPOCHJD;
var Registry = require('../registry');
var utcFormat = d3.time.format.utc;
var DATETIME_REGEXP = /^\s*(-?\d\d\d\d|\d\d)(-(\d?\d)(-(\d?\d)([ Tt]([01]?\d|2[0-3])(:([0-5]\d)(:([0-5]\d(\.\d+)?))?(Z|z|[+\-]\d\d:?\d\d)?)?)?)?)?\s*$/m;
// special regex for chinese calendars to support yyyy-mmi-dd etc for intercalary months
var DATETIME_REGEXP_CN = /^\s*(-?\d\d\d\d|\d\d)(-(\d?\di?)(-(\d?\d)([ Tt]([01]?\d|2[0-3])(:([0-5]\d)(:([0-5]\d(\.\d+)?))?(Z|z|[+\-]\d\d:?\d\d)?)?)?)?)?\s*$/m;
// for 2-digit years, the first year we map them onto
var YFIRST = new Date().getFullYear() - 70;
function isWorldCalendar(calendar) {
return (
calendar &&
Registry.componentsRegistry.calendars &&
typeof calendar === 'string' && calendar !== 'gregorian'
);
}
/*
* dateTick0: get the canonical tick for this calendar
*
* bool sunday is for week ticks, shift it to a Sunday.
*/
exports.dateTick0 = function(calendar, sunday) {
if(isWorldCalendar(calendar)) {
return sunday ?
Registry.getComponentMethod('calendars', 'CANONICAL_SUNDAY')[calendar] :
Registry.getComponentMethod('calendars', 'CANONICAL_TICK')[calendar];
}
else {
return sunday ? '2000-01-02' : '2000-01-01';
}
};
/*
* dfltRange: for each calendar, give a valid default range
*/
exports.dfltRange = function(calendar) {
if(isWorldCalendar(calendar)) {
return Registry.getComponentMethod('calendars', 'DFLTRANGE')[calendar];
}
else {
return ['2000-01-01', '2001-01-01'];
}
};
// is an object a javascript date?
exports.isJSDate = function(v) {
return typeof v === 'object' && v !== null && typeof v.getTime === 'function';
};
// The absolute limits of our date-time system
// This is a little weird: we use MIN_MS and MAX_MS in dateTime2ms
// but we use dateTime2ms to calculate them (after defining it!)
var MIN_MS, MAX_MS;
/**
* dateTime2ms - turn a date object or string s into milliseconds
* (relative to 1970-01-01, per javascript standard)
* optional calendar (string) to use a non-gregorian calendar
*
* Returns BADNUM if it doesn't find a date
*
* strings should have the form:
*
* -?YYYY-mm-dd<sep>HH:MM:SS.sss<tzInfo>?
*
* <sep>: space (our normal standard) or T or t (ISO-8601)
* <tzInfo>: Z, z, or [+\-]HH:?MM and we THROW IT AWAY
* this format comes from https://tools.ietf.org/html/rfc3339#section-5.6
* but we allow it even with a space as the separator
*
* May truncate after any full field, and sss can be any length
* even >3 digits, though javascript dates truncate to milliseconds,
* we keep as much as javascript numeric precision can hold, but we only
* report back up to 100 microsecond precision, because most dates support
* this precision (close to 1970 support more, very far away support less)
*
* Expanded to support negative years to -9999 but you must always
* give 4 digits, except for 2-digit positive years which we assume are
* near the present time.
* Note that we follow ISO 8601:2004: there *is* a year 0, which
* is 1BC/BCE, and -1===2BC etc.
*
* World calendars: not all of these *have* agreed extensions to this full range,
* if you have another calendar system but want a date range outside its validity,
* you can use a gregorian date string prefixed with 'G' or 'g'.
*
* Where to cut off 2-digit years between 1900s and 2000s?
* from http://support.microsoft.com/kb/244664:
* 1930-2029 (the most retro of all...)
* but in my mac chrome from eg. d=new Date(Date.parse('8/19/50')):
* 1950-2049
* by Java, from http://stackoverflow.com/questions/2024273/:
* now-80 - now+19
* or FileMaker Pro, from
* http://www.filemaker.com/12help/html/add_view_data.4.21.html:
* now-70 - now+29
* but python strptime etc, via
* http://docs.python.org/py3k/library/time.html:
* 1969-2068 (super forward-looking, but static, not sliding!)
*
* lets go with now-70 to now+29, and if anyone runs into this problem
* they can learn the hard way not to use 2-digit years, as no choice we
* make now will cover all possibilities. mostly this will all be taken
* care of in initial parsing, should only be an issue for hand-entered data
* currently (2016) this range is:
* 1946-2045
*/
exports.dateTime2ms = function(s, calendar) {
// first check if s is a date object
Iif(exports.isJSDate(s)) {
// Convert to the UTC milliseconds that give the same
// hours as this date has in the local timezone
s = Number(s) - s.getTimezoneOffset() * ONEMIN;
if(s >= MIN_MS && s <= MAX_MS) return s;
return BADNUM;
}
// otherwise only accept strings and numbers
Iif(typeof s !== 'string' && typeof s !== 'number') return BADNUM;
s = String(s);
var isWorld = isWorldCalendar(calendar);
// to handle out-of-range dates in international calendars, accept
// 'G' as a prefix to force the built-in gregorian calendar.
var s0 = s.charAt(0);
Iif(isWorld && (s0 === 'G' || s0 === 'g')) {
s = s.substr(1);
calendar = '';
}
var isChinese = isWorld && calendar.substr(0, 7) === 'chinese';
var match = s.match(isChinese ? DATETIME_REGEXP_CN : DATETIME_REGEXP);
Iif(!match) return BADNUM;
var y = match[1],
m = match[3] || '1',
d = Number(match[5] || 1),
H = Number(match[7] || 0),
M = Number(match[9] || 0),
S = Number(match[11] || 0);
Iif(isWorld) {
// disallow 2-digit years for world calendars
if(y.length === 2) return BADNUM;
y = Number(y);
var cDate;
try {
var calInstance = Registry.getComponentMethod('calendars', 'getCal')(calendar);
if(isChinese) {
var isIntercalary = m.charAt(m.length - 1) === 'i';
m = parseInt(m, 10);
cDate = calInstance.newDate(y, calInstance.toMonthIndex(y, m, isIntercalary), d);
}
else {
cDate = calInstance.newDate(y, Number(m), d);
}
}
catch(e) { return BADNUM; } // Invalid ... date
if(!cDate) return BADNUM;
return ((cDate.toJD() - EPOCHJD) * ONEDAY) +
(H * ONEHOUR) + (M * ONEMIN) + (S * ONESEC);
}
Iif(y.length === 2) {
y = (Number(y) + 2000 - YFIRST) % 100 + YFIRST;
}
else y = Number(y);
// new Date uses months from 0; subtract 1 here just so we
// don't have to do it again during the validity test below
m -= 1;
// javascript takes new Date(0..99,m,d) to mean 1900-1999, so
// to support years 0-99 we need to use setFullYear explicitly
// Note that 2000 is a leap year.
var date = new Date(Date.UTC(2000, m, d, H, M));
date.setUTCFullYear(y);
Iif(date.getUTCMonth() !== m) return BADNUM;
Iif(date.getUTCDate() !== d) return BADNUM;
return date.getTime() + S * ONESEC;
};
MIN_MS = exports.MIN_MS = exports.dateTime2ms('-9999');
MAX_MS = exports.MAX_MS = exports.dateTime2ms('9999-12-31 23:59:59.9999');
// is string s a date? (see above)
exports.isDateTime = function(s, calendar) {
return (exports.dateTime2ms(s, calendar) !== BADNUM);
};
// pad a number with zeroes, to given # of digits before the decimal point
function lpad(val, digits) {
return String(val + Math.pow(10, digits)).substr(1);
}
/**
* Turn ms into string of the form YYYY-mm-dd HH:MM:SS.ssss
* Crop any trailing zeros in time, except never stop right after hours
* (we could choose to crop '-01' from date too but for now we always
* show the whole date)
* Optional range r is the data range that applies, also in ms.
* If rng is big, the later parts of time will be omitted
*/
var NINETYDAYS = 90 * ONEDAY;
var THREEHOURS = 3 * ONEHOUR;
var FIVEMIN = 5 * ONEMIN;
exports.ms2DateTime = function(ms, r, calendar) {
if(typeof ms !== 'number' || !(ms >= MIN_MS && ms <= MAX_MS)) return BADNUM;
if(!r) r = 0;
var msecTenths = Math.floor(mod(ms + 0.05, 1) * 10),
msRounded = Math.round(ms - msecTenths / 10),
dateStr, h, m, s, msec10, d;
if(isWorldCalendar(calendar)) {
var dateJD = Math.floor(msRounded / ONEDAY) + EPOCHJD,
timeMs = Math.floor(mod(ms, ONEDAY));
try {
dateStr = Registry.getComponentMethod('calendars', 'getCal')(calendar)
.fromJD(dateJD).formatDate('yyyy-mm-dd');
}
catch(e) {
// invalid date in this calendar - fall back to Gyyyy-mm-dd
dateStr = utcFormat('G%Y-%m-%d')(new Date(msRounded));
}
// yyyy does NOT guarantee 4-digit years. YYYY mostly does, but does
// other things for a few calendars, so we can't trust it. Just pad
// it manually (after the '-' if there is one)
if(dateStr.charAt(0) === '-') {
while(dateStr.length < 11) dateStr = '-0' + dateStr.substr(1);
}
else {
while(dateStr.length < 10) dateStr = '0' + dateStr;
}
// TODO: if this is faster, we could use this block for extracting
// the time components of regular gregorian too
h = (r < NINETYDAYS) ? Math.floor(timeMs / ONEHOUR) : 0;
m = (r < NINETYDAYS) ? Math.floor((timeMs % ONEHOUR) / ONEMIN) : 0;
s = (r < THREEHOURS) ? Math.floor((timeMs % ONEMIN) / ONESEC) : 0;
msec10 = (r < FIVEMIN) ? (timeMs % ONESEC) * 10 + msecTenths : 0;
}
else {
d = new Date(msRounded);
dateStr = utcFormat('%Y-%m-%d')(d);
// <90 days: add hours and minutes - never *only* add hours
h = (r < NINETYDAYS) ? d.getUTCHours() : 0;
m = (r < NINETYDAYS) ? d.getUTCMinutes() : 0;
// <3 hours: add seconds
s = (r < THREEHOURS) ? d.getUTCSeconds() : 0;
// <5 minutes: add ms (plus one extra digit, this is msec*10)
msec10 = (r < FIVEMIN) ? d.getUTCMilliseconds() * 10 + msecTenths : 0;
}
return includeTime(dateStr, h, m, s, msec10);
};
// For converting old-style milliseconds to date strings,
// we use the local timezone rather than UTC like we use
// everywhere else, both for backward compatibility and
// because that's how people mostly use javasript date objects.
// Clip one extra day off our date range though so we can't get
// thrown beyond the range by the timezone shift.
exports.ms2DateTimeLocal = function(ms) {
if(!(ms >= MIN_MS + ONEDAY && ms <= MAX_MS - ONEDAY)) return BADNUM;
var msecTenths = Math.floor(mod(ms + 0.05, 1) * 10),
d = new Date(Math.round(ms - msecTenths / 10)),
dateStr = d3.time.format('%Y-%m-%d')(d),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds(),
msec10 = d.getUTCMilliseconds() * 10 + msecTenths;
return includeTime(dateStr, h, m, s, msec10);
};
function includeTime(dateStr, h, m, s, msec10) {
// include each part that has nonzero data in or after it
if(h || m || s || msec10) {
dateStr += ' ' + lpad(h, 2) + ':' + lpad(m, 2);
if(s || msec10) {
dateStr += ':' + lpad(s, 2);
if(msec10) {
var digits = 4;
while(msec10 % 10 === 0) {
digits -= 1;
msec10 /= 10;
}
dateStr += '.' + lpad(msec10, digits);
}
}
}
return dateStr;
}
// normalize date format to date string, in case it starts as
// a Date object or milliseconds
// optional dflt is the return value if cleaning fails
exports.cleanDate = function(v, dflt, calendar) {
if(exports.isJSDate(v) || typeof v === 'number') {
// do not allow milliseconds (old) or jsdate objects (inherently
// described as gregorian dates) with world calendars
if(isWorldCalendar(calendar)) {
logError('JS Dates and milliseconds are incompatible with world calendars', v);
return dflt;
}
// NOTE: if someone puts in a year as a number rather than a string,
// this will mistakenly convert it thinking it's milliseconds from 1970
// that is: '2012' -> Jan. 1, 2012, but 2012 -> 2012 epoch milliseconds
v = exports.ms2DateTimeLocal(+v);
if(!v && dflt !== undefined) return dflt;
}
else if(!exports.isDateTime(v, calendar)) {
logError('unrecognized date', v);
return dflt;
}
return v;
};
/*
* Date formatting for ticks and hovertext
*/
/*
* modDateFormat: Support world calendars, and add one item to
* d3's vocabulary:
* %{n}f where n is the max number of digits of fractional seconds
*/
var fracMatch = /%\d?f/g;
function modDateFormat(fmt, x, calendar) {
fmt = fmt.replace(fracMatch, function(match) {
var digits = Math.min(+(match.charAt(1)) || 6, 6),
fracSecs = ((x / 1000 % 1) + 2)
.toFixed(digits)
.substr(2).replace(/0+$/, '') || '0';
return fracSecs;
});
var d = new Date(Math.floor(x + 0.05));
if(isWorldCalendar(calendar)) {
try {
fmt = Registry.getComponentMethod('calendars', 'worldCalFmt')(fmt, x, calendar);
}
catch(e) {
return 'Invalid';
}
}
return utcFormat(fmt)(d);
}
/*
* formatTime: create a time string from:
* x: milliseconds
* tr: tickround ('M', 'S', or # digits)
* only supports UTC times (where every day is 24 hours and 0 is at midnight)
*/
var MAXSECONDS = [59, 59.9, 59.99, 59.999, 59.9999];
function formatTime(x, tr) {
var timePart = mod(x + 0.05, ONEDAY);
var timeStr = lpad(Math.floor(timePart / ONEHOUR), 2) + ':' +
lpad(mod(Math.floor(timePart / ONEMIN), 60), 2);
if(tr !== 'M') {
if(!isNumeric(tr)) tr = 0; // should only be 'S'
/*
* this is a weird one - and shouldn't come up unless people
* monkey with tick0 in weird ways, but we need to do something!
* IN PARTICULAR we had better not display garbage (see below)
* for numbers we always round to the nearest increment of the
* precision we're showing, and this seems like the right way to
* handle seconds and milliseconds, as they have a decimal point
* and people will interpret that to mean rounding like numbers.
* but for larger increments we floor the value: it's always
* 2013 until the ball drops on the new year. We could argue about
* which field it is where we start rounding (should 12:08:59
* round to 12:09 if we're stopping at minutes?) but for now I'll
* say we round seconds but floor everything else. BUT that means
* we need to never round up to 60 seconds, ie 23:59:60
*/
var sec = Math.min(mod(x / ONESEC, 60), MAXSECONDS[tr]);
var secStr = (100 + sec).toFixed(tr).substr(1);
if(tr > 0) {
secStr = secStr.replace(/0+$/, '').replace(/[\.]$/, '');
}
timeStr += ':' + secStr;
}
return timeStr;
}
var yearFormat = utcFormat('%Y'),
monthFormat = utcFormat('%b %Y'),
dayFormat = utcFormat('%b %-d'),
yearMonthDayFormat = utcFormat('%b %-d, %Y');
function yearFormatWorld(cDate) { return cDate.formatDate('yyyy'); }
function monthFormatWorld(cDate) { return cDate.formatDate('M yyyy'); }
function dayFormatWorld(cDate) { return cDate.formatDate('M d'); }
function yearMonthDayFormatWorld(cDate) { return cDate.formatDate('M d, yyyy'); }
/*
* formatDate: turn a date into tick or hover label text.
*
* x: milliseconds, the value to convert
* fmt: optional, an explicit format string (d3 format, even for world calendars)
* tr: tickround ('y', 'm', 'd', 'M', 'S', or # digits)
* used if no explicit fmt is provided
* calendar: optional string, the world calendar system to use
*
* returns the date/time as a string, potentially with the leading portion
* on a separate line (after '\n')
* Note that this means if you provide an explicit format which includes '\n'
* the axis may choose to strip things after it when they don't change from
* one tick to the next (as it does with automatic formatting)
*/
exports.formatDate = function(x, fmt, tr, calendar) {
var headStr,
dateStr;
calendar = isWorldCalendar(calendar) && calendar;
if(fmt) return modDateFormat(fmt, x, calendar);
if(calendar) {
try {
var dateJD = Math.floor((x + 0.05) / ONEDAY) + EPOCHJD,
cDate = Registry.getComponentMethod('calendars', 'getCal')(calendar)
.fromJD(dateJD);
if(tr === 'y') dateStr = yearFormatWorld(cDate);
else if(tr === 'm') dateStr = monthFormatWorld(cDate);
else if(tr === 'd') {
headStr = yearFormatWorld(cDate);
dateStr = dayFormatWorld(cDate);
}
else {
headStr = yearMonthDayFormatWorld(cDate);
dateStr = formatTime(x, tr);
}
}
catch(e) { return 'Invalid'; }
}
else {
var d = new Date(Math.floor(x + 0.05));
if(tr === 'y') dateStr = yearFormat(d);
else if(tr === 'm') dateStr = monthFormat(d);
else if(tr === 'd') {
headStr = yearFormat(d);
dateStr = dayFormat(d);
}
else {
headStr = yearMonthDayFormat(d);
dateStr = formatTime(x, tr);
}
}
return dateStr + (headStr ? '\n' + headStr : '');
};
/*
* incrementMonth: make a new milliseconds value from the given one,
* having changed the month
*
* special case for world calendars: multiples of 12 are treated as years,
* even for calendar systems that don't have (always or ever) 12 months/year
* TODO: perhaps we need a different code for year increments to support this?
*
* ms (number): the initial millisecond value
* dMonth (int): the (signed) number of months to shift
* calendar (string): the calendar system to use
*
* changing month does not (and CANNOT) always preserve day, since
* months have different lengths. The worst example of this is:
* d = new Date(1970,0,31); d.setMonth(1) -> Feb 31 turns into Mar 3
*
* But we want to be able to iterate over the last day of each month,
* regardless of what its number is.
* So shift 3 days forward, THEN set the new month, then unshift:
* 1/31 -> 2/28 (or 29) -> 3/31 -> 4/30 -> ...
*
* Note that odd behavior still exists if you start from the 26th-28th:
* 1/28 -> 2/28 -> 3/31
* but at least you can't shift any dates into the wrong month,
* and ticks on these days incrementing by month would be very unusual
*/
var THREEDAYS = 3 * ONEDAY;
exports.incrementMonth = function(ms, dMonth, calendar) {
calendar = isWorldCalendar(calendar) && calendar;
// pull time out and operate on pure dates, then add time back at the end
// this gives maximum precision - not that we *normally* care if we're
// incrementing by month, but better to be safe!
var timeMs = mod(ms, ONEDAY);
ms = Math.round(ms - timeMs);
if(calendar) {
try {
var dateJD = Math.round(ms / ONEDAY) + EPOCHJD,
calInstance = Registry.getComponentMethod('calendars', 'getCal')(calendar),
cDate = calInstance.fromJD(dateJD);
if(dMonth % 12) calInstance.add(cDate, dMonth, 'm');
else calInstance.add(cDate, dMonth / 12, 'y');
return (cDate.toJD() - EPOCHJD) * ONEDAY + timeMs;
}
catch(e) {
logError('invalid ms ' + ms + ' in calendar ' + calendar);
// then keep going in gregorian even though the result will be 'Invalid'
}
}
var y = new Date(ms + THREEDAYS);
return y.setUTCMonth(y.getUTCMonth() + dMonth) + timeMs - THREEDAYS;
};
/*
* findExactDates: what fraction of data is exact days, months, or years?
*
* data: array of millisecond values
* calendar (string) the calendar to test against
*/
exports.findExactDates = function(data, calendar) {
var exactYears = 0,
exactMonths = 0,
exactDays = 0,
blankCount = 0,
d,
di;
var calInstance = (
isWorldCalendar(calendar) &&
Registry.getComponentMethod('calendars', 'getCal')(calendar)
);
for(var i = 0; i < data.length; i++) {
di = data[i];
// not date data at all
if(!isNumeric(di)) {
blankCount ++;
continue;
}
// not an exact date
if(di % ONEDAY) continue;
if(calInstance) {
try {
d = calInstance.fromJD(di / ONEDAY + EPOCHJD);
if(d.day() === 1) {
if(d.month() === 1) exactYears++;
else exactMonths++;
}
else exactDays++;
}
catch(e) {
// invalid date in this calendar - ignore it here.
}
}
else {
d = new Date(di);
if(d.getUTCDate() === 1) {
if(d.getUTCMonth() === 0) exactYears++;
else exactMonths++;
}
else exactDays++;
}
}
exactMonths += exactYears;
exactDays += exactMonths;
var dataCount = data.length - blankCount;
return {
exactYears: exactYears / dataCount,
exactMonths: exactMonths / dataCount,
exactDays: exactDays / dataCount
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Ensures an array has the right amount of storage space. If it doesn't * exist, it creates an array. If it does exist, it returns it if too * short or truncates it in-place. * * The goal is to just reuse memory to avoid a bit of excessive garbage * collection. */ module.exports = function ensureArray(out, n) { if(!Array.isArray(out)) out = []; // If too long, truncate. (If too short, it will grow // automatically so we don't care about that case) out.length = n; return out; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/* global jQuery:false */
var EventEmitter = require('events').EventEmitter;
var Events = {
init: function(plotObj) {
/*
* If we have already instantiated an emitter for this plot
* return early.
*/
if(plotObj._ev instanceof EventEmitter) return plotObj;
var ev = new EventEmitter();
var internalEv = new EventEmitter();
/*
* Assign to plot._ev while we still live in a land
* where plot is a DOM element with stuff attached to it.
* In the future we can make plot the event emitter itself.
*/
plotObj._ev = ev;
/*
* Create a second event handler that will manage events *internally*.
* This allows parts of plotly to respond to thing like relayout without
* having to use the user-facing event handler. They cannot peacefully
* coexist on the same handler because a user invoking
* plotObj.removeAllListeners() would detach internal events, breaking
* plotly.
*/
plotObj._internalEv = internalEv;
/*
* Assign bound methods from the ev to the plot object. These methods
* will reference the 'this' of plot._ev even though they are methods
* of plot. This will keep the event machinery away from the plot object
* which currently is often a DOM element but presents an API that will
* continue to function when plot becomes an emitter. Not all EventEmitter
* methods have been bound to `plot` as some do not currently add value to
* the Plotly event API.
*/
plotObj.on = ev.on.bind(ev);
plotObj.once = ev.once.bind(ev);
plotObj.removeListener = ev.removeListener.bind(ev);
plotObj.removeAllListeners = ev.removeAllListeners.bind(ev);
/*
* Create funtions for managing internal events. These are *only* triggered
* by the mirroring of external events via the emit function.
*/
plotObj._internalOn = internalEv.on.bind(internalEv);
plotObj._internalOnce = internalEv.once.bind(internalEv);
plotObj._removeInternalListener = internalEv.removeListener.bind(internalEv);
plotObj._removeAllInternalListeners = internalEv.removeAllListeners.bind(internalEv);
/*
* We must wrap emit to continue to support JQuery events. The idea
* is to check to see if the user is using JQuery events, if they are
* we emit JQuery events to trigger user handlers as well as the EventEmitter
* events.
*/
plotObj.emit = function(event, data) {
if(typeof jQuery !== 'undefined') {
jQuery(plotObj).trigger(event, data);
}
ev.emit(event, data);
internalEv.emit(event, data);
};
return plotObj;
},
/*
* This function behaves like jQueries triggerHandler. It calls
* all handlers for a particular event and returns the return value
* of the LAST handler. This function also triggers jQuery's
* triggerHandler for backwards compatibility.
*
* Note: triggerHandler has been recommended for deprecation in v2.0.0,
* so the additional behavior of triggerHandler triggering internal events
* is deliberate excluded in order to avoid reinforcing more usage.
*/
triggerHandler: function(plotObj, event, data) {
var jQueryHandlerValue;
var nodeEventHandlerValue;
/*
* If Jquery exists run all its handlers for this event and
* collect the return value of the LAST handler function
*/
if(typeof jQuery !== 'undefined') {
jQueryHandlerValue = jQuery(plotObj).triggerHandler(event, data);
}
/*
* Now run all the node style event handlers
*/
var ev = plotObj._ev;
if(!ev) return jQueryHandlerValue;
var handlers = ev._events[event];
if(!handlers) return jQueryHandlerValue;
/*
* handlers can be function or an array of functions
*/
if(typeof handlers === 'function') handlers = [handlers];
var lastHandler = handlers.pop();
/*
* Call all the handlers except the last one.
*/
for(var i = 0; i < handlers.length; i++) {
handlers[i](data);
}
/*
* Now call the final handler and collect its value
*/
nodeEventHandlerValue = lastHandler(data);
/*
* Return either the jquery handler value if it exists or the
* nodeEventHandler value. Jquery event value superceeds nodejs
* events for backwards compatability reasons.
*/
return jQueryHandlerValue !== undefined ? jQueryHandlerValue :
nodeEventHandlerValue;
},
purge: function(plotObj) {
delete plotObj._ev;
delete plotObj.on;
delete plotObj.once;
delete plotObj.removeListener;
delete plotObj.removeAllListeners;
delete plotObj.emit;
delete plotObj._ev;
delete plotObj._internalEv;
delete plotObj._internalOn;
delete plotObj._internalOnce;
delete plotObj._removeInternalListener;
delete plotObj._removeAllInternalListeners;
return plotObj;
}
};
module.exports = Events;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 1 1 1 1 47 1 62 1 1 1 109 109 109 109 150 150 400 400 400 400 338 338 47 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isPlainObject = require('./is_plain_object.js');
var isArray = Array.isArray;
function primitivesLoopSplice(source, target) {
var i, value;
for(i = 0; i < source.length; i++) {
value = source[i];
if(value !== null && typeof(value) === 'object') {
return false;
}
if(value !== void(0)) {
target[i] = value;
}
}
return true;
}
exports.extendFlat = function() {
return _extend(arguments, false, false, false);
};
exports.extendDeep = function() {
return _extend(arguments, true, false, false);
};
exports.extendDeepAll = function() {
return _extend(arguments, true, true, false);
};
exports.extendDeepNoArrays = function() {
return _extend(arguments, true, false, true);
};
/*
* Inspired by https://github.com/justmoon/node-extend/blob/master/index.js
* All credit to the jQuery authors for perfecting this amazing utility.
*
* API difference with jQuery version:
* - No optional boolean (true -> deep extend) first argument,
* use `extendFlat` for first-level only extend and
* use `extendDeep` for a deep extend.
*
* Other differences with jQuery version:
* - Uses a modern (and faster) isPlainObject routine.
* - Expected to work with object {} and array [] arguments only.
* - Does not check for circular structure.
* FYI: jQuery only does a check across one level.
* Warning: this might result in infinite loops.
*
*/
function _extend(inputs, isDeep, keepAllKeys, noArrayCopies) {
var target = inputs[0],
length = inputs.length;
var input, key, src, copy, copyIsArray, clone, allPrimitives;
Iif(length === 2 && isArray(target) && isArray(inputs[1]) && target.length === 0) {
allPrimitives = primitivesLoopSplice(inputs[1], target);
if(allPrimitives) {
return target;
} else {
target.splice(0, target.length); // reset target and continue to next block
}
}
for(var i = 1; i < length; i++) {
input = inputs[i];
for(key in input) {
src = target[key];
copy = input[key];
// Stop early and just transfer the array if array copies are disallowed:
Iif(noArrayCopies && isArray(copy)) {
target[key] = copy;
}
// recurse if we're merging plain objects or arrays
else Iif(isDeep && copy && (isPlainObject(copy) || (copyIsArray = isArray(copy)))) {
if(copyIsArray) {
copyIsArray = false;
clone = src && isArray(src) ? src : [];
} else {
clone = src && isPlainObject(src) ? src : {};
}
// never move original objects, clone them
target[key] = _extend([clone, copy], isDeep, keepAllKeys, noArrayCopies);
}
// don't bring in undefined values, except for extendDeepAll
else Eif(typeof copy !== 'undefined' || keepAllKeys) {
target[key] = copy;
}
}
}
return target;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Return news array containing only the unique items
* found in input array.
*
* IMPORTANT: Note that items are considered unique
* if `String({})` is unique. For example;
*
* Lib.filterUnique([ { a: 1 }, { b: 2 } ])
*
* returns [{ a: 1 }]
*
* and
*
* Lib.filterUnique([ '1', 1 ])
*
* returns ['1']
*
*
* @param {array} array base array
* @return {array} new filtered array
*/
module.exports = function filterUnique(array) {
var seen = {},
out = [],
j = 0;
for(var i = 0; i < array.length; i++) {
var item = array[i];
if(seen[item] !== 1) {
seen[item] = 1;
out[j++] = item;
}
}
return out;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/** Filter out object items with visible !== true
* insider array container.
*
* @param {array of objects} container
* @return {array of objects} of length <= container
*
*/
module.exports = function filterVisible(container) {
var out = [];
for(var i = 0; i < container.length; i++) {
var item = container[i];
if(item.visible === true) out.push(item);
}
return out;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var countryRegex = require('country-regex');
var Lib = require('../lib');
// make list of all country iso3 ids from at runtime
var countryIds = Object.keys(countryRegex);
var locationmodeToIdFinder = {
'ISO-3': Lib.identity,
'USA-states': Lib.identity,
'country names': countryNameToISO3
};
exports.locationToFeature = function(locationmode, location, features) {
var locationId = getLocationId(locationmode, location);
if(locationId) {
for(var i = 0; i < features.length; i++) {
var feature = features[i];
if(feature.id === locationId) return feature;
}
Lib.warn([
'Location with id', locationId,
'does not have a matching topojson feature at this resolution.'
].join(' '));
}
return false;
};
function getLocationId(locationmode, location) {
var idFinder = locationmodeToIdFinder[locationmode];
return idFinder(location);
}
function countryNameToISO3(countryName) {
for(var i = 0; i < countryIds.length; i++) {
var iso3 = countryIds[i],
regex = new RegExp(countryRegex[iso3]);
if(regex.test(countryName.trim().toLowerCase())) return iso3;
}
Lib.warn('Unrecognized country name: ' + countryName + '.');
return false;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var BADNUM = require('../constants/numerical').BADNUM;
/**
* Convert calcTrace to GeoJSON 'MultiLineString' coordinate arrays
*
* @param {object} calcTrace
* gd.calcdata item.
* Note that calcTrace[i].lonlat is assumed to be defined
*
* @return {array}
* return line coords array (or array of arrays)
*
*/
exports.calcTraceToLineCoords = function(calcTrace) {
var trace = calcTrace[0].trace;
var connectgaps = trace.connectgaps;
var coords = [];
var lineString = [];
for(var i = 0; i < calcTrace.length; i++) {
var calcPt = calcTrace[i];
var lonlat = calcPt.lonlat;
if(lonlat[0] !== BADNUM) {
lineString.push(lonlat);
} else if(!connectgaps && lineString.length > 0) {
coords.push(lineString);
lineString = [];
}
}
if(lineString.length > 0) {
coords.push(lineString);
}
return coords;
};
/**
* Make line ('LineString' or 'MultiLineString') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*
*/
exports.makeLine = function(coords, trace) {
var out = {};
if(coords.length === 1) {
out = {
type: 'LineString',
coordinates: coords[0]
};
}
else {
out = {
type: 'MultiLineString',
coordinates: coords
};
}
if(trace) out.trace = trace;
return out;
};
/**
* Make polygon ('Polygon' or 'MultiPolygon') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*/
exports.makePolygon = function(coords, trace) {
var out = {};
if(coords.length === 1) {
out = {
type: 'Polygon',
coordinates: coords
};
}
else {
var _coords = new Array(coords.length);
for(var i = 0; i < coords.length; i++) {
_coords[i] = [coords[i]];
}
out = {
type: 'MultiPolygon',
coordinates: _coords
};
}
if(trace) out.trace = trace;
return out;
};
/**
* Make blank GeoJSON
*
* @return {object}
* Blank GeoJSON object
*
*/
exports.makeBlank = function() {
return {
type: 'Point',
coordinates: []
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var rgba = require('color-rgba');
var Colorscale = require('../components/colorscale');
var colorDflt = require('../components/color/attributes').defaultLine;
var colorDfltRgba = rgba(colorDflt);
var opacityDflt = 1;
function calculateColor(colorIn, opacityIn) {
var colorOut = colorIn;
colorOut[3] *= opacityIn;
return colorOut;
}
function validateColor(colorIn) {
if(isNumeric(colorIn)) return colorDfltRgba;
var colorOut = rgba(colorIn);
return colorOut.length ? colorOut : colorDfltRgba;
}
function validateOpacity(opacityIn) {
return isNumeric(opacityIn) ? opacityIn : opacityDflt;
}
function formatColor(containerIn, opacityIn, len) {
var colorIn = containerIn.color,
isArrayColorIn = Array.isArray(colorIn),
isArrayOpacityIn = Array.isArray(opacityIn),
colorOut = [];
var sclFunc, getColor, getOpacity, colori, opacityi;
if(containerIn.colorscale !== undefined) {
sclFunc = Colorscale.makeColorScaleFunc(
Colorscale.extractScale(
containerIn.colorscale,
containerIn.cmin,
containerIn.cmax
)
);
}
else {
sclFunc = validateColor;
}
if(isArrayColorIn) {
getColor = function(c, i) {
return c[i] === undefined ? colorDfltRgba : rgba(sclFunc(c[i]));
};
}
else getColor = validateColor;
if(isArrayOpacityIn) {
getOpacity = function(o, i) {
return o[i] === undefined ? opacityDflt : validateOpacity(o[i]);
};
}
else getOpacity = validateOpacity;
if(isArrayColorIn || isArrayOpacityIn) {
for(var i = 0; i < len; i++) {
colori = getColor(colorIn, i);
opacityi = getOpacity(opacityIn, i);
colorOut[i] = calculateColor(colori, opacityi);
}
}
else colorOut = calculateColor(rgba(colorIn), opacityIn);
return colorOut;
}
module.exports = formatColor;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var toSuperScript = require('superscript-text');
var stringMappings = require('../constants/string_mappings');
function fixSuperScript(x) {
var idx = 0;
while((idx = x.indexOf('<sup>', idx)) >= 0) {
var nidx = x.indexOf('</sup>', idx);
if(nidx < idx) break;
x = x.slice(0, idx) + toSuperScript(x.slice(idx + 5, nidx)) + x.slice(nidx + 6);
}
return x;
}
function fixBR(x) {
return x.replace(/\<br\>/g, '\n');
}
function stripTags(x) {
return x.replace(/\<.*\>/g, '');
}
function fixEntities(x) {
var entityToUnicode = stringMappings.entityToUnicode;
var idx = 0;
while((idx = x.indexOf('&', idx)) >= 0) {
var nidx = x.indexOf(';', idx);
if(nidx < idx) {
idx += 1;
continue;
}
var entity = entityToUnicode[x.slice(idx + 1, nidx)];
if(entity) {
x = x.slice(0, idx) + entity + x.slice(nidx + 1);
} else {
x = x.slice(0, idx) + x.slice(nidx + 1);
}
}
return x;
}
function convertHTMLToUnicode(html) {
return '' +
fixEntities(
stripTags(
fixSuperScript(
fixBR(
html))));
}
module.exports = convertHTMLToUnicode;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // Simple helper functions // none of these need any external deps module.exports = function identity(d) { return d; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20 20 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var lib = module.exports = {};
lib.nestedProperty = require('./nested_property');
lib.isPlainObject = require('./is_plain_object');
lib.isArray = require('./is_array');
lib.mod = require('./mod');
lib.toLogRange = require('./to_log_range');
lib.relinkPrivateKeys = require('./relink_private');
lib.ensureArray = require('./ensure_array');
var coerceModule = require('./coerce');
lib.valObjects = coerceModule.valObjects;
lib.coerce = coerceModule.coerce;
lib.coerce2 = coerceModule.coerce2;
lib.coerceFont = coerceModule.coerceFont;
lib.validate = coerceModule.validate;
var datesModule = require('./dates');
lib.dateTime2ms = datesModule.dateTime2ms;
lib.isDateTime = datesModule.isDateTime;
lib.ms2DateTime = datesModule.ms2DateTime;
lib.ms2DateTimeLocal = datesModule.ms2DateTimeLocal;
lib.cleanDate = datesModule.cleanDate;
lib.isJSDate = datesModule.isJSDate;
lib.formatDate = datesModule.formatDate;
lib.incrementMonth = datesModule.incrementMonth;
lib.dateTick0 = datesModule.dateTick0;
lib.dfltRange = datesModule.dfltRange;
lib.findExactDates = datesModule.findExactDates;
lib.MIN_MS = datesModule.MIN_MS;
lib.MAX_MS = datesModule.MAX_MS;
var searchModule = require('./search');
lib.findBin = searchModule.findBin;
lib.sorterAsc = searchModule.sorterAsc;
lib.sorterDes = searchModule.sorterDes;
lib.distinctVals = searchModule.distinctVals;
lib.roundUp = searchModule.roundUp;
var statsModule = require('./stats');
lib.aggNums = statsModule.aggNums;
lib.len = statsModule.len;
lib.mean = statsModule.mean;
lib.variance = statsModule.variance;
lib.stdev = statsModule.stdev;
lib.interp = statsModule.interp;
var matrixModule = require('./matrix');
lib.init2dArray = matrixModule.init2dArray;
lib.transposeRagged = matrixModule.transposeRagged;
lib.dot = matrixModule.dot;
lib.translationMatrix = matrixModule.translationMatrix;
lib.rotationMatrix = matrixModule.rotationMatrix;
lib.rotationXYMatrix = matrixModule.rotationXYMatrix;
lib.apply2DTransform = matrixModule.apply2DTransform;
lib.apply2DTransform2 = matrixModule.apply2DTransform2;
var extendModule = require('./extend');
lib.extendFlat = extendModule.extendFlat;
lib.extendDeep = extendModule.extendDeep;
lib.extendDeepAll = extendModule.extendDeepAll;
lib.extendDeepNoArrays = extendModule.extendDeepNoArrays;
var loggersModule = require('./loggers');
lib.log = loggersModule.log;
lib.warn = loggersModule.warn;
lib.error = loggersModule.error;
lib.notifier = require('./notifier');
lib.filterUnique = require('./filter_unique');
lib.filterVisible = require('./filter_visible');
lib.pushUnique = require('./push_unique');
lib.cleanNumber = require('./clean_number');
lib.noop = require('./noop');
lib.identity = require('./identity');
/**
* swap x and y of the same attribute in container cont
* specify attr with a ? in place of x/y
* you can also swap other things than x/y by providing part1 and part2
*/
lib.swapAttrs = function(cont, attrList, part1, part2) {
if(!part1) part1 = 'x';
if(!part2) part2 = 'y';
for(var i = 0; i < attrList.length; i++) {
var attr = attrList[i],
xp = lib.nestedProperty(cont, attr.replace('?', part1)),
yp = lib.nestedProperty(cont, attr.replace('?', part2)),
temp = xp.get();
xp.set(yp.get());
yp.set(temp);
}
};
/**
* to prevent event bubbling, in particular text selection during drag.
* see http://stackoverflow.com/questions/5429827/
* how-can-i-prevent-text-element-selection-with-cursor-drag
* for maximum effect use:
* return pauseEvent(e);
*/
lib.pauseEvent = function(e) {
if(e.stopPropagation) e.stopPropagation();
if(e.preventDefault) e.preventDefault();
e.cancelBubble = true;
return false;
};
// constrain - restrict a number v to be between v0 and v1
lib.constrain = function(v, v0, v1) {
if(v0 > v1) return Math.max(v1, Math.min(v0, v));
return Math.max(v0, Math.min(v1, v));
};
/**
* do two bounding boxes from getBoundingClientRect,
* ie {left,right,top,bottom,width,height}, overlap?
* takes optional padding pixels
*/
lib.bBoxIntersect = function(a, b, pad) {
pad = pad || 0;
return (a.left <= b.right + pad &&
b.left <= a.right + pad &&
a.top <= b.bottom + pad &&
b.top <= a.bottom + pad);
};
/*
* simpleMap: alternative to Array.map that only
* passes on the element and up to 2 extra args you
* provide (but not the array index or the whole array)
*
* array: the array to map it to
* func: the function to apply
* x1, x2: optional extra args
*/
lib.simpleMap = function(array, func, x1, x2) {
var len = array.length,
out = new Array(len);
for(var i = 0; i < len; i++) out[i] = func(array[i], x1, x2);
return out;
};
// random string generator
lib.randstr = function randstr(existing, bits, base) {
/*
* Include number of bits, the base of the string you want
* and an optional array of existing strings to avoid.
*/
if(!base) base = 16;
if(bits === undefined) bits = 24;
if(bits <= 0) return '0';
var digits = Math.log(Math.pow(2, bits)) / Math.log(base),
res = '',
i,
b,
x;
for(i = 2; digits === Infinity; i *= 2) {
digits = Math.log(Math.pow(2, bits / i)) / Math.log(base) * i;
}
var rem = digits - Math.floor(digits);
for(i = 0; i < Math.floor(digits); i++) {
x = Math.floor(Math.random() * base).toString(base);
res = x + res;
}
if(rem) {
b = Math.pow(base, rem);
x = Math.floor(Math.random() * b).toString(base);
res = x + res;
}
var parsed = parseInt(res, base);
if((existing && (existing.indexOf(res) > -1)) ||
(parsed !== Infinity && parsed >= Math.pow(2, bits))) {
return randstr(existing, bits, base);
}
else return res;
};
lib.OptionControl = function(opt, optname) {
/*
* An environment to contain all option setters and
* getters that collectively modify opts.
*
* You can call up opts from any function in new object
* as this.optname || this.opt
*
* See FitOpts for example of usage
*/
if(!opt) opt = {};
if(!optname) optname = 'opt';
var self = {};
self.optionList = [];
self._newoption = function(optObj) {
optObj[optname] = opt;
self[optObj.name] = optObj;
self.optionList.push(optObj);
};
self['_' + optname] = opt;
return self;
};
/**
* lib.smooth: smooth arrayIn by convolving with
* a hann window with given full width at half max
* bounce the ends in, so the output has the same length as the input
*/
lib.smooth = function(arrayIn, FWHM) {
FWHM = Math.round(FWHM) || 0; // only makes sense for integers
if(FWHM < 2) return arrayIn;
var alen = arrayIn.length,
alen2 = 2 * alen,
wlen = 2 * FWHM - 1,
w = new Array(wlen),
arrayOut = new Array(alen),
i,
j,
k,
v;
// first make the window array
for(i = 0; i < wlen; i++) {
w[i] = (1 - Math.cos(Math.PI * (i + 1) / FWHM)) / (2 * FWHM);
}
// now do the convolution
for(i = 0; i < alen; i++) {
v = 0;
for(j = 0; j < wlen; j++) {
k = i + j + 1 - FWHM;
// multibounce
if(k < -alen) k -= alen2 * Math.round(k / alen2);
else if(k >= alen2) k -= alen2 * Math.floor(k / alen2);
// single bounce
if(k < 0) k = - 1 - k;
else if(k >= alen) k = alen2 - 1 - k;
v += arrayIn[k] * w[j];
}
arrayOut[i] = v;
}
return arrayOut;
};
/**
* syncOrAsync: run a sequence of functions synchronously
* as long as its returns are not promises (ie have no .then)
* includes one argument arg to send to all functions...
* this is mainly just to prevent us having to make wrapper functions
* when the only purpose of the wrapper is to reference gd
* and a final step to be executed at the end
* TODO: if there's an error and everything is sync,
* this doesn't happen yet because we want to make sure
* that it gets reported
*/
lib.syncOrAsync = function(sequence, arg, finalStep) {
var ret, fni;
function continueAsync() {
return lib.syncOrAsync(sequence, arg, finalStep);
}
while(sequence.length) {
fni = sequence.splice(0, 1)[0];
ret = fni(arg);
if(ret && ret.then) {
return ret.then(continueAsync)
.then(undefined, lib.promiseError);
}
}
return finalStep && finalStep(arg);
};
/**
* Helper to strip trailing slash, from
* http://stackoverflow.com/questions/6680825/return-string-without-trailing-slash
*/
lib.stripTrailingSlash = function(str) {
if(str.substr(-1) === '/') return str.substr(0, str.length - 1);
return str;
};
lib.noneOrAll = function(containerIn, containerOut, attrList) {
/**
* some attributes come together, so if you have one of them
* in the input, you should copy the default values of the others
* to the input as well.
*/
if(!containerIn) return;
var hasAny = false,
hasAll = true,
i,
val;
for(i = 0; i < attrList.length; i++) {
val = containerIn[attrList[i]];
if(val !== undefined && val !== null) hasAny = true;
else hasAll = false;
}
if(hasAny && !hasAll) {
for(i = 0; i < attrList.length; i++) {
containerIn[attrList[i]] = containerOut[attrList[i]];
}
}
};
lib.mergeArray = function(traceAttr, cd, cdAttr) {
if(Array.isArray(traceAttr)) {
var imax = Math.min(traceAttr.length, cd.length);
for(var i = 0; i < imax; i++) cd[i][cdAttr] = traceAttr[i];
}
};
/**
* modified version of jQuery's extend to strip out private objs and functions,
* and cut arrays down to first <arraylen> or 1 elements
* because extend-like algorithms are hella slow
* obj2 is assumed to already be clean of these things (including no arrays)
*/
lib.minExtend = function(obj1, obj2) {
var objOut = {};
if(typeof obj2 !== 'object') obj2 = {};
var arrayLen = 3,
keys = Object.keys(obj1),
i,
k,
v;
for(i = 0; i < keys.length; i++) {
k = keys[i];
v = obj1[k];
if(k.charAt(0) === '_' || typeof v === 'function') continue;
else if(k === 'module') objOut[k] = v;
else if(Array.isArray(v)) objOut[k] = v.slice(0, arrayLen);
else if(v && (typeof v === 'object')) objOut[k] = lib.minExtend(obj1[k], obj2[k]);
else objOut[k] = v;
}
keys = Object.keys(obj2);
for(i = 0; i < keys.length; i++) {
k = keys[i];
v = obj2[k];
if(typeof v !== 'object' || !(k in objOut) || typeof objOut[k] !== 'object') {
objOut[k] = v;
}
}
return objOut;
};
lib.titleCase = function(s) {
return s.charAt(0).toUpperCase() + s.substr(1);
};
lib.containsAny = function(s, fragments) {
for(var i = 0; i < fragments.length; i++) {
if(s.indexOf(fragments[i]) !== -1) return true;
}
return false;
};
// get the parent Plotly plot of any element. Whoo jquery-free tree climbing!
lib.getPlotDiv = function(el) {
for(; el && el.removeAttribute; el = el.parentNode) {
if(lib.isPlotDiv(el)) return el;
}
};
lib.isPlotDiv = function(el) {
var el3 = d3.select(el);
return el3.node() instanceof HTMLElement &&
el3.size() &&
el3.classed('js-plotly-plot');
};
lib.removeElement = function(el) {
var elParent = el && el.parentNode;
if(elParent) elParent.removeChild(el);
};
/**
* for dynamically adding style rules
* makes one stylesheet that contains all rules added
* by all calls to this function
*/
lib.addStyleRule = function(selector, styleString) {
Eif(!lib.styleSheet) {
var style = document.createElement('style');
// WebKit hack :(
style.appendChild(document.createTextNode(''));
document.head.appendChild(style);
lib.styleSheet = style.sheet;
}
var styleSheet = lib.styleSheet;
if(styleSheet.insertRule) {
styleSheet.insertRule(selector + '{' + styleString + '}', 0);
}
else if(styleSheet.addRule) {
styleSheet.addRule(selector, styleString, 0);
}
else lib.warn('addStyleRule failed');
};
lib.isIE = function() {
return typeof window.navigator.msSaveBlob !== 'undefined';
};
/**
* Duck typing to recognize a d3 selection, mostly for IE9's benefit
* because it doesn't handle instanceof like modern browsers
*/
lib.isD3Selection = function(obj) {
return obj && (typeof obj.classed === 'function');
};
/**
* Converts a string path to an object.
*
* When given a string containing an array element, it will create a `null`
* filled array of the given size.
*
* @example
* lib.objectFromPath('nested.test[2].path', 'value');
* // returns { nested: { test: [null, null, { path: 'value' }]}
*
* @param {string} path to nested value
* @param {*} any value to be set
*
* @return {Object} the constructed object with a full nested path
*/
lib.objectFromPath = function(path, value) {
var keys = path.split('.'),
tmpObj,
obj = tmpObj = {};
for(var i = 0; i < keys.length; i++) {
var key = keys[i];
var el = null;
var parts = keys[i].match(/(.*)\[([0-9]+)\]/);
if(parts) {
key = parts[1];
el = parts[2];
tmpObj = tmpObj[key] = [];
if(i === keys.length - 1) {
tmpObj[el] = value;
} else {
tmpObj[el] = {};
}
tmpObj = tmpObj[el];
} else {
if(i === keys.length - 1) {
tmpObj[key] = value;
} else {
tmpObj[key] = {};
}
tmpObj = tmpObj[key];
}
}
return obj;
};
/**
* Iterate through an object in-place, converting dotted properties to objects.
*
* Examples:
*
* lib.expandObjectPaths({'nested.test.path': 'value'});
* => { nested: { test: {path: 'value'}}}
*
* It also handles array notation, e.g.:
*
* lib.expandObjectPaths({'foo[1].bar': 'value'});
* => { foo: [null, {bar: value}] }
*
* It handles merges the results when two properties are specified in parallel:
*
* lib.expandObjectPaths({'foo[1].bar': 10, 'foo[0].bar': 20});
* => { foo: [{bar: 10}, {bar: 20}] }
*
* It does NOT, however, merge mulitple mutliply-nested arrays::
*
* lib.expandObjectPaths({'marker[1].range[1]': 5, 'marker[1].range[0]': 4})
* => { marker: [null, {range: 4}] }
*/
// Store this to avoid recompiling regex on *every* prop since this may happen many
// many times for animations. Could maybe be inside the function. Not sure about
// scoping vs. recompilation tradeoff, but at least it's not just inlining it into
// the inner loop.
var dottedPropertyRegex = /^([^\[\.]+)\.(.+)?/;
var indexedPropertyRegex = /^([^\.]+)\[([0-9]+)\](\.)?(.+)?/;
lib.expandObjectPaths = function(data) {
var match, key, prop, datum, idx, dest, trailingPath;
if(typeof data === 'object' && !Array.isArray(data)) {
for(key in data) {
if(data.hasOwnProperty(key)) {
if((match = key.match(dottedPropertyRegex))) {
datum = data[key];
prop = match[1];
delete data[key];
data[prop] = lib.extendDeepNoArrays(data[prop] || {}, lib.objectFromPath(key, lib.expandObjectPaths(datum))[prop]);
} else if((match = key.match(indexedPropertyRegex))) {
datum = data[key];
prop = match[1];
idx = parseInt(match[2]);
delete data[key];
data[prop] = data[prop] || [];
if(match[3] === '.') {
// This is the case where theere are subsequent properties into which
// we must recurse, e.g. transforms[0].value
trailingPath = match[4];
dest = data[prop][idx] = data[prop][idx] || {};
// NB: Extend deep no arrays prevents this from working on multiple
// nested properties in the same object, e.g.
//
// {
// foo[0].bar[1].range
// foo[0].bar[0].range
// }
//
// In this case, the extendDeepNoArrays will overwrite one array with
// the other, so that both properties *will not* be present in the
// result. Fixing this would require a more intelligent tracking
// of changes and merging than extendDeepNoArrays currently accomplishes.
lib.extendDeepNoArrays(dest, lib.objectFromPath(trailingPath, lib.expandObjectPaths(datum)));
} else {
// This is the case where this property is the end of the line,
// e.g. xaxis.range[0]
data[prop][idx] = lib.expandObjectPaths(datum);
}
} else {
data[key] = lib.expandObjectPaths(data[key]);
}
}
}
}
return data;
};
/**
* Converts value to string separated by the provided separators.
*
* @example
* lib.numSeparate(2016, '.,');
* // returns '2016'
*
* @example
* lib.numSeparate(3000, '.,', true);
* // returns '3,000'
*
* @example
* lib.numSeparate(1234.56, '|,')
* // returns '1,234|56'
*
* @param {string|number} value the value to be converted
* @param {string} separators string of decimal, then thousands separators
* @param {boolean} separatethousands boolean, 4-digit integers are separated if true
*
* @return {string} the value that has been separated
*/
lib.numSeparate = function(value, separators, separatethousands) {
if(!separatethousands) separatethousands = false;
if(typeof separators !== 'string' || separators.length === 0) {
throw new Error('Separator string required for formatting!');
}
if(typeof value === 'number') {
value = String(value);
}
var thousandsRe = /(\d+)(\d{3})/,
decimalSep = separators.charAt(0),
thouSep = separators.charAt(1);
var x = value.split('.'),
x1 = x[0],
x2 = x.length > 1 ? decimalSep + x[1] : '';
// Years are ignored for thousands separators
if(thouSep && (x.length > 1 || x1.length > 4 || separatethousands)) {
while(thousandsRe.test(x1)) {
x1 = x1.replace(thousandsRe, '$1' + thouSep + '$2');
}
}
return x1 + x2;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /** * Return true for arrays, whether they're untyped or not. */ // IE9 fallback var ab = (typeof ArrayBuffer === 'undefined' || !ArrayBuffer.isView) ? {isView: function() { return false; }} : ArrayBuffer; module.exports = function isArray(a) { return Array.isArray(a) || ab.isView(a); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 62 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// more info: http://stackoverflow.com/questions/18531624/isplainobject-thing
module.exports = function isPlainObject(obj) {
// We need to be a little less strict in the `imagetest` container because
// of how async image requests are handled.
//
// N.B. isPlainObject(new Constructor()) will return true in `imagetest`
if(window && window.process && window.process.versions) {
return Object.prototype.toString.call(obj) === '[object Object]';
}
return (
Object.prototype.toString.call(obj) === '[object Object]' &&
Object.getPrototypeOf(obj) === Object.prototype
);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/* eslint-disable no-console */
var config = require('../plot_api/plot_config');
var loggers = module.exports = {};
/**
* ------------------------------------------
* debugging tools
* ------------------------------------------
*/
loggers.log = function() {
if(config.logging > 1) {
var messages = ['LOG:'];
for(var i = 0; i < arguments.length; i++) {
messages.push(arguments[i]);
}
apply(console.trace || console.log, messages);
}
};
loggers.warn = function() {
if(config.logging > 0) {
var messages = ['WARN:'];
for(var i = 0; i < arguments.length; i++) {
messages.push(arguments[i]);
}
apply(console.trace || console.log, messages);
}
};
loggers.error = function() {
if(config.logging > 0) {
var messages = ['ERROR:'];
for(var i = 0; i < arguments.length; i++) {
messages.push(arguments[i]);
}
apply(console.error, messages);
}
};
/*
* Robust apply, for IE9 where console.log doesn't support
* apply like other functions do
*/
function apply(f, args) {
if(f.apply) {
f.apply(f, args);
}
else {
for(var i = 0; i < args.length; i++) {
f(args[i]);
}
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1 1 1 1 1 1 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; exports.init2dArray = function(rowLength, colLength) { var array = new Array(rowLength); for(var i = 0; i < rowLength; i++) array[i] = new Array(colLength); return array; }; /** * transpose a (possibly ragged) 2d array z. inspired by * http://stackoverflow.com/questions/17428587/ * transposing-a-2d-array-in-javascript */ exports.transposeRagged = function(z) { var maxlen = 0, zlen = z.length, i, j; // Maximum row length: for(i = 0; i < zlen; i++) maxlen = Math.max(maxlen, z[i].length); var t = new Array(maxlen); for(i = 0; i < maxlen; i++) { t[i] = new Array(zlen); for(j = 0; j < zlen; j++) t[i][j] = z[j][i]; } return t; }; // our own dot function so that we don't need to include numeric exports.dot = function(x, y) { if(!(x.length && y.length) || x.length !== y.length) return null; var len = x.length, out, i; if(x[0].length) { // mat-vec or mat-mat out = new Array(len); for(i = 0; i < len; i++) out[i] = exports.dot(x[i], y); } else if(y[0].length) { // vec-mat var yTranspose = exports.transposeRagged(y); out = new Array(yTranspose.length); for(i = 0; i < yTranspose.length; i++) out[i] = exports.dot(x, yTranspose[i]); } else { // vec-vec out = 0; for(i = 0; i < len; i++) out += x[i] * y[i]; } return out; }; // translate by (x,y) exports.translationMatrix = function(x, y) { return [[1, 0, x], [0, 1, y], [0, 0, 1]]; }; // rotate by alpha around (0,0) exports.rotationMatrix = function(alpha) { var a = alpha * Math.PI / 180; return [[Math.cos(a), -Math.sin(a), 0], [Math.sin(a), Math.cos(a), 0], [0, 0, 1]]; }; // rotate by alpha around (x,y) exports.rotationXYMatrix = function(a, x, y) { return exports.dot( exports.dot(exports.translationMatrix(x, y), exports.rotationMatrix(a)), exports.translationMatrix(-x, -y)); }; // applies a 2D transformation matrix to either x and y params or an [x,y] array exports.apply2DTransform = function(transform) { return function() { var args = arguments; if(args.length === 3) { args = args[0]; }// from map var xy = arguments.length === 1 ? args[0] : [args[0], args[1]]; return exports.dot(transform, [xy[0], xy[1], 1]).slice(0, 2); }; }; // applies a 2D transformation matrix to an [x1,y1,x2,y2] array (to transform a segment) exports.apply2DTransform2 = function(transform) { var at = exports.apply2DTransform(transform); return function(xys) { return at(xys.slice(0, 2)).concat(at(xys.slice(2, 4))); }; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /** * sanitized modulus function that always returns in the range [0, d) * rather than (-d, 0] if v is negative */ module.exports = function mod(v, d) { var out = v % d; return out < 0 ? out + d : out; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var isArray = require('./is_array');
var isPlainObject = require('./is_plain_object');
var containerArrayMatch = require('../plot_api/container_array_match');
/**
* convert a string s (such as 'xaxis.range[0]')
* representing a property of nested object into set and get methods
* also return the string and object so we don't have to keep track of them
* allows [-1] for an array index, to set a property inside all elements
* of an array
* eg if obj = {arr: [{a: 1}, {a: 2}]}
* you can do p = nestedProperty(obj, 'arr[-1].a')
* but you cannot set the array itself this way, to do that
* just set the whole array.
* eg if obj = {arr: [1, 2, 3]}
* you can't do nestedProperty(obj, 'arr[-1]').set(5)
* but you can do nestedProperty(obj, 'arr').set([5, 5, 5])
*/
module.exports = function nestedProperty(container, propStr) {
if(isNumeric(propStr)) propStr = String(propStr);
else if(typeof propStr !== 'string' ||
propStr.substr(propStr.length - 4) === '[-1]') {
throw 'bad property string';
}
var j = 0,
propParts = propStr.split('.'),
indexed,
indices,
i;
// check for parts of the nesting hierarchy that are numbers (ie array elements)
while(j < propParts.length) {
// look for non-bracket chars, then any number of [##] blocks
indexed = String(propParts[j]).match(/^([^\[\]]*)((\[\-?[0-9]*\])+)$/);
if(indexed) {
if(indexed[1]) propParts[j] = indexed[1];
// allow propStr to start with bracketed array indices
else if(j === 0) propParts.splice(0, 1);
else throw 'bad property string';
indices = indexed[2]
.substr(1, indexed[2].length - 2)
.split('][');
for(i = 0; i < indices.length; i++) {
j++;
propParts.splice(j, 0, Number(indices[i]));
}
}
j++;
}
if(typeof container !== 'object') {
return badContainer(container, propStr, propParts);
}
return {
set: npSet(container, propParts, propStr),
get: npGet(container, propParts),
astr: propStr,
parts: propParts,
obj: container
};
};
function npGet(cont, parts) {
return function() {
var curCont = cont,
curPart,
allSame,
out,
i,
j;
for(i = 0; i < parts.length - 1; i++) {
curPart = parts[i];
if(curPart === -1) {
allSame = true;
out = [];
for(j = 0; j < curCont.length; j++) {
out[j] = npGet(curCont[j], parts.slice(i + 1))();
if(out[j] !== out[0]) allSame = false;
}
return allSame ? out[0] : out;
}
if(typeof curPart === 'number' && !isArray(curCont)) {
return undefined;
}
curCont = curCont[curPart];
if(typeof curCont !== 'object' || curCont === null) {
return undefined;
}
}
// only hit this if parts.length === 1
if(typeof curCont !== 'object' || curCont === null) return undefined;
out = curCont[parts[i]];
if(out === null) return undefined;
return out;
};
}
/*
* Can this value be deleted? We can delete any empty object (null, undefined, [], {})
* EXCEPT empty data arrays, {} inside an array, or anything INSIDE an *args* array.
*
* Info arrays can be safely deleted, but not deleting them has no ill effects other
* than leaving a trace or layout object with some cruft in it.
*
* Deleting data arrays can change the meaning of the object, as `[]` means there is
* data for this attribute, it's just empty right now while `undefined` means the data
* should be filled in with defaults to match other data arrays.
*
* `{}` inside an array means "the default object" which is clearly different from
* popping it off the end of the array, or setting it `undefined` inside the array.
*
* *args* arrays get passed directly to API methods and we should respect precisely
* what the user has put there - although if the whole *args* array is empty it's fine
* to delete that.
*
* So we do some simple tests here to find known non-data arrays but don't worry too
* much about not deleting some arrays that would actually be safe to delete.
*/
var INFO_PATTERNS = /(^|\.)((domain|range)(\.[xy])?|args|parallels)$/;
var ARGS_PATTERN = /(^|\.)args\[/;
function isDeletable(val, propStr) {
if(!emptyObj(val) ||
(isPlainObject(val) && propStr.charAt(propStr.length - 1) === ']') ||
(propStr.match(ARGS_PATTERN) && val !== undefined)
) {
return false;
}
if(!isArray(val)) return true;
if(propStr.match(INFO_PATTERNS)) return true;
var match = containerArrayMatch(propStr);
// if propStr matches the container array itself, index is an empty string
// otherwise we've matched something inside the container array, which may
// still be a data array.
return match && (match.index === '');
}
function npSet(cont, parts, propStr) {
return function(val) {
var curCont = cont,
propPart = '',
containerLevels = [[cont, propPart]],
toDelete = isDeletable(val, propStr),
curPart,
i;
for(i = 0; i < parts.length - 1; i++) {
curPart = parts[i];
if(typeof curPart === 'number' && !isArray(curCont)) {
throw 'array index but container is not an array';
}
// handle special -1 array index
if(curPart === -1) {
toDelete = !setArrayAll(curCont, parts.slice(i + 1), val, propStr);
if(toDelete) break;
else return;
}
if(!checkNewContainer(curCont, curPart, parts[i + 1], toDelete)) {
break;
}
curCont = curCont[curPart];
if(typeof curCont !== 'object' || curCont === null) {
throw 'container is not an object';
}
propPart = joinPropStr(propPart, curPart);
containerLevels.push([curCont, propPart]);
}
if(toDelete) {
if(i === parts.length - 1) delete curCont[parts[i]];
pruneContainers(containerLevels);
}
else curCont[parts[i]] = val;
};
}
function joinPropStr(propStr, newPart) {
var toAdd = newPart;
if(isNumeric(newPart)) toAdd = '[' + newPart + ']';
else if(propStr) toAdd = '.' + newPart;
return propStr + toAdd;
}
// handle special -1 array index
function setArrayAll(containerArray, innerParts, val, propStr) {
var arrayVal = isArray(val),
allSet = true,
thisVal = val,
thisPropStr = propStr.replace('-1', 0),
deleteThis = arrayVal ? false : isDeletable(val, thisPropStr),
firstPart = innerParts[0],
i;
for(i = 0; i < containerArray.length; i++) {
thisPropStr = propStr.replace('-1', i);
if(arrayVal) {
thisVal = val[i % val.length];
deleteThis = isDeletable(thisVal, thisPropStr);
}
if(deleteThis) allSet = false;
if(!checkNewContainer(containerArray, i, firstPart, deleteThis)) {
continue;
}
npSet(containerArray[i], innerParts, propStr.replace('-1', i))(thisVal);
}
return allSet;
}
/**
* make new sub-container as needed.
* returns false if there's no container and none is needed
* because we're only deleting an attribute
*/
function checkNewContainer(container, part, nextPart, toDelete) {
if(container[part] === undefined) {
if(toDelete) return false;
if(typeof nextPart === 'number') container[part] = [];
else container[part] = {};
}
return true;
}
function pruneContainers(containerLevels) {
var i,
j,
curCont,
propPart,
keys,
remainingKeys;
for(i = containerLevels.length - 1; i >= 0; i--) {
curCont = containerLevels[i][0];
propPart = containerLevels[i][1];
remainingKeys = false;
if(isArray(curCont)) {
for(j = curCont.length - 1; j >= 0; j--) {
if(isDeletable(curCont[j], joinPropStr(propPart, j))) {
if(remainingKeys) curCont[j] = undefined;
else curCont.pop();
}
else remainingKeys = true;
}
}
else if(typeof curCont === 'object' && curCont !== null) {
keys = Object.keys(curCont);
remainingKeys = false;
for(j = keys.length - 1; j >= 0; j--) {
if(isDeletable(curCont[keys[j]], joinPropStr(propPart, keys[j]))) {
delete curCont[keys[j]];
}
else remainingKeys = true;
}
}
if(remainingKeys) return;
}
}
function emptyObj(obj) {
if(obj === undefined || obj === null) return true;
if(typeof obj !== 'object') return false; // any plain value
if(isArray(obj)) return !obj.length; // []
return !Object.keys(obj).length; // {}
}
function badContainer(container, propStr, propParts) {
return {
set: function() { throw 'bad container'; },
get: function() {},
astr: propStr,
parts: propParts,
obj: container
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// Simple helper functions
// none of these need any external deps
module.exports = function noop() {};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var NOTEDATA = [];
/**
* notifier
* @param {String} text The person's user name
* @param {Number} [delay=1000] The delay time in milliseconds
* or 'long' which provides 2000 ms delay time.
* @return {undefined} this function does not return a value
*/
module.exports = function(text, displayLength) {
if(NOTEDATA.indexOf(text) !== -1) return;
NOTEDATA.push(text);
var ts = 1000;
if(isNumeric(displayLength)) ts = displayLength;
else if(displayLength === 'long') ts = 3000;
var notifierContainer = d3.select('body')
.selectAll('.plotly-notifier')
.data([0]);
notifierContainer.enter()
.append('div')
.classed('plotly-notifier', true);
var notes = notifierContainer.selectAll('.notifier-note').data(NOTEDATA);
function killNote(transition) {
transition
.duration(700)
.style('opacity', 0)
.each('end', function(thisText) {
var thisIndex = NOTEDATA.indexOf(thisText);
if(thisIndex !== -1) NOTEDATA.splice(thisIndex, 1);
d3.select(this).remove();
});
}
notes.enter().append('div')
.classed('notifier-note', true)
.style('opacity', 0)
.each(function(thisText) {
var note = d3.select(this);
note.append('button')
.classed('notifier-close', true)
.html('×')
.on('click', function() {
note.transition().call(killNote);
});
var p = note.append('p');
var lines = thisText.split(/<br\s*\/?>/g);
for(var i = 0; i < lines.length; i++) {
if(i) p.append('br');
p.append('span').text(lines[i]);
}
note.transition()
.duration(700)
.style('opacity', 1)
.transition()
.delay(ts)
.call(killNote);
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var setCursor = require('./setcursor');
var STASHATTR = 'data-savedcursor';
var NO_CURSOR = '!!';
/*
* works with our CSS cursor classes (see css/_cursor.scss)
* to override a previous cursor set on d3 single-element selections,
* by moving the name of the original cursor to the data-savedcursor attr.
* omit cursor to revert to the previously set value.
*/
module.exports = function overrideCursor(el3, csr) {
var savedCursor = el3.attr(STASHATTR);
if(csr) {
if(!savedCursor) {
var classes = (el3.attr('class') || '').split(' ');
for(var i = 0; i < classes.length; i++) {
var cls = classes[i];
if(cls.indexOf('cursor-') === 0) {
el3.attr(STASHATTR, cls.substr(7))
.classed(cls, false);
}
}
if(!el3.attr(STASHATTR)) {
el3.attr(STASHATTR, NO_CURSOR);
}
}
setCursor(el3, csr);
}
else if(savedCursor) {
el3.attr(STASHATTR, null);
if(savedCursor === NO_CURSOR) setCursor(el3);
else setCursor(el3, savedCursor);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var dot = require('./matrix').dot;
var polygon = module.exports = {};
/**
* Turn an array of [x, y] pairs into a polygon object
* that can test if points are inside it
*
* @param ptsIn Array of [x, y] pairs
*
* @returns polygon Object {xmin, xmax, ymin, ymax, pts, contains}
* (x|y)(min|max) are the bounding rect of the polygon
* pts is the original array, with the first pair repeated at the end
* contains is a function: (pt, omitFirstEdge)
* pt is the [x, y] pair to test
* omitFirstEdge truthy means points exactly on the first edge don't
* count. This is for use adding one polygon to another so we
* don't double-count the edge where they meet.
* returns boolean: is pt inside the polygon (including on its edges)
*/
polygon.tester = function tester(ptsIn) {
var pts = ptsIn.slice(),
xmin = pts[0][0],
xmax = xmin,
ymin = pts[0][1],
ymax = ymin;
pts.push(pts[0]);
for(var i = 1; i < pts.length; i++) {
xmin = Math.min(xmin, pts[i][0]);
xmax = Math.max(xmax, pts[i][0]);
ymin = Math.min(ymin, pts[i][1]);
ymax = Math.max(ymax, pts[i][1]);
}
// do we have a rectangle? Handle this here, so we can use the same
// tester for the rectangular case without sacrificing speed
var isRect = false,
rectFirstEdgeTest;
if(pts.length === 5) {
if(pts[0][0] === pts[1][0]) { // vert, horz, vert, horz
if(pts[2][0] === pts[3][0] &&
pts[0][1] === pts[3][1] &&
pts[1][1] === pts[2][1]) {
isRect = true;
rectFirstEdgeTest = function(pt) { return pt[0] === pts[0][0]; };
}
}
else if(pts[0][1] === pts[1][1]) { // horz, vert, horz, vert
if(pts[2][1] === pts[3][1] &&
pts[0][0] === pts[3][0] &&
pts[1][0] === pts[2][0]) {
isRect = true;
rectFirstEdgeTest = function(pt) { return pt[1] === pts[0][1]; };
}
}
}
function rectContains(pt, omitFirstEdge) {
var x = pt[0],
y = pt[1];
if(x < xmin || x > xmax || y < ymin || y > ymax) {
// pt is outside the bounding box of polygon
return false;
}
if(omitFirstEdge && rectFirstEdgeTest(pt)) return false;
return true;
}
function contains(pt, omitFirstEdge) {
var x = pt[0],
y = pt[1];
if(x < xmin || x > xmax || y < ymin || y > ymax) {
// pt is outside the bounding box of polygon
return false;
}
var imax = pts.length,
x1 = pts[0][0],
y1 = pts[0][1],
crossings = 0,
i,
x0,
y0,
xmini,
ycross;
for(i = 1; i < imax; i++) {
// find all crossings of a vertical line upward from pt with
// polygon segments
// crossings exactly at xmax don't count, unless the point is
// exactly on the segment, then it counts as inside.
x0 = x1;
y0 = y1;
x1 = pts[i][0];
y1 = pts[i][1];
xmini = Math.min(x0, x1);
// outside the bounding box of this segment, it's only a crossing
// if it's below the box.
if(x < xmini || x > Math.max(x0, x1) || y > Math.max(y0, y1)) {
continue;
}
else if(y < Math.min(y0, y1)) {
// don't count the left-most point of the segment as a crossing
// because we don't want to double-count adjacent crossings
// UNLESS the polygon turns past vertical at exactly this x
// Note that this is repeated below, but we can't factor it out
// because
if(x !== xmini) crossings++;
}
// inside the bounding box, check the actual line intercept
else {
// vertical segment - we know already that the point is exactly
// on the segment, so mark the crossing as exactly at the point.
if(x1 === x0) ycross = y;
// any other angle
else ycross = y0 + (x - x0) * (y1 - y0) / (x1 - x0);
// exactly on the edge: counts as inside the polygon, unless it's the
// first edge and we're omitting it.
if(y === ycross) {
if(i === 1 && omitFirstEdge) return false;
return true;
}
if(y <= ycross && x !== xmini) crossings++;
}
}
// if we've gotten this far, odd crossings means inside, even is outside
return crossings % 2 === 1;
}
return {
xmin: xmin,
xmax: xmax,
ymin: ymin,
ymax: ymax,
pts: pts,
contains: isRect ? rectContains : contains,
isRect: isRect
};
};
/**
* Test if a segment of a points array is bent or straight
*
* @param pts Array of [x, y] pairs
* @param start the index of the proposed start of the straight section
* @param end the index of the proposed end point
* @param tolerance the max distance off the line connecting start and end
* before the line counts as bent
* @returns boolean: true means this segment is bent, false means straight
*/
var isBent = polygon.isSegmentBent = function isBent(pts, start, end, tolerance) {
var startPt = pts[start],
segment = [pts[end][0] - startPt[0], pts[end][1] - startPt[1]],
segmentSquared = dot(segment, segment),
segmentLen = Math.sqrt(segmentSquared),
unitPerp = [-segment[1] / segmentLen, segment[0] / segmentLen],
i,
part,
partParallel;
for(i = start + 1; i < end; i++) {
part = [pts[i][0] - startPt[0], pts[i][1] - startPt[1]];
partParallel = dot(part, segment);
if(partParallel < 0 || partParallel > segmentSquared ||
Math.abs(dot(part, unitPerp)) > tolerance) return true;
}
return false;
};
/**
* Make a filtering polygon, to minimize the number of segments
*
* @param pts Array of [x, y] pairs (must start with at least 1 pair)
* @param tolerance the maximum deviation from straight allowed for
* removing points to simplify the polygon
*
* @returns Object {addPt, raw, filtered}
* addPt is a function(pt: [x, y] pair) to add a raw point and
* continue filtering
* raw is all the input points
* filtered is the resulting filtered Array of [x, y] pairs
*/
polygon.filter = function filter(pts, tolerance) {
var ptsFiltered = [pts[0]],
doneRawIndex = 0,
doneFilteredIndex = 0;
function addPt(pt) {
pts.push(pt);
var prevFilterLen = ptsFiltered.length,
iLast = doneRawIndex;
ptsFiltered.splice(doneFilteredIndex + 1);
for(var i = iLast + 1; i < pts.length; i++) {
if(i === pts.length - 1 || isBent(pts, iLast, i + 1, tolerance)) {
ptsFiltered.push(pts[i]);
if(ptsFiltered.length < prevFilterLen - 2) {
doneRawIndex = i;
doneFilteredIndex = ptsFiltered.length - 1;
}
iLast = i;
}
}
}
if(pts.length > 1) {
var lastPt = pts.pop();
addPt(lastPt);
}
return {
addPt: addPt,
raw: pts,
filtered: ptsFiltered
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Push array with unique items
*
* @param {array} array
* array to be filled
* @param {any} item
* item to be or not to be inserted
* @return {array}
* ref to array (now possibly containing one more item)
*
*/
module.exports = function pushUnique(array, item) {
if(item instanceof RegExp) {
var itemStr = item.toString(),
i;
for(i = 0; i < array.length; i++) {
if(array[i] instanceof RegExp && array[i].toString() === itemStr) {
return array;
}
}
array.push(item);
}
else if(item && array.indexOf(item) === -1) array.push(item);
return array;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var config = require('../plot_api/plot_config');
/**
* Copy arg array *without* removing `undefined` values from objects.
*
* @param gd
* @param args
* @returns {Array}
*/
function copyArgArray(gd, args) {
var copy = [];
var arg;
for(var i = 0; i < args.length; i++) {
arg = args[i];
if(arg === gd) copy[i] = arg;
else if(typeof arg === 'object') {
copy[i] = Array.isArray(arg) ?
Lib.extendDeep([], arg) :
Lib.extendDeepAll({}, arg);
}
else copy[i] = arg;
}
return copy;
}
// -----------------------------------------------------
// Undo/Redo queue for plots
// -----------------------------------------------------
var queue = {};
// TODO: disable/enable undo and redo buttons appropriately
/**
* Add an item to the undoQueue for a graphDiv
*
* @param gd
* @param undoFunc Function undo this operation
* @param undoArgs Args to supply undoFunc with
* @param redoFunc Function to redo this operation
* @param redoArgs Args to supply redoFunc with
*/
queue.add = function(gd, undoFunc, undoArgs, redoFunc, redoArgs) {
var queueObj,
queueIndex;
// make sure we have the queue and our position in it
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
queueIndex = gd.undoQueue.index;
// if we're already playing an undo or redo, or if this is an auto operation
// (like pane resize... any others?) then we don't save this to the undo queue
if(gd.autoplay) {
if(!gd.undoQueue.inSequence) gd.autoplay = false;
return;
}
// if we're not in a sequence or are just starting, we need a new queue item
if(!gd.undoQueue.sequence || gd.undoQueue.beginSequence) {
queueObj = {undo: {calls: [], args: []}, redo: {calls: [], args: []}};
gd.undoQueue.queue.splice(queueIndex, gd.undoQueue.queue.length - queueIndex, queueObj);
gd.undoQueue.index += 1;
} else {
queueObj = gd.undoQueue.queue[queueIndex - 1];
}
gd.undoQueue.beginSequence = false;
// we unshift to handle calls for undo in a forward for loop later
if(queueObj) {
queueObj.undo.calls.unshift(undoFunc);
queueObj.undo.args.unshift(undoArgs);
queueObj.redo.calls.push(redoFunc);
queueObj.redo.args.push(redoArgs);
}
if(gd.undoQueue.queue.length > config.queueLength) {
gd.undoQueue.queue.shift();
gd.undoQueue.index--;
}
};
/**
* Begin a sequence of undoQueue changes
*
* @param gd
*/
queue.startSequence = function(gd) {
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
gd.undoQueue.sequence = true;
gd.undoQueue.beginSequence = true;
};
/**
* Stop a sequence of undoQueue changes
*
* Call this *after* you're sure your undo chain has ended
*
* @param gd
*/
queue.stopSequence = function(gd) {
gd.undoQueue = gd.undoQueue || {index: 0, queue: [], sequence: false};
gd.undoQueue.sequence = false;
gd.undoQueue.beginSequence = false;
};
/**
* Move one step back in the undo queue, and undo the object there.
*
* @param gd
*/
queue.undo = function undo(gd) {
var queueObj, i;
if(gd.framework && gd.framework.isPolar) {
gd.framework.undo();
return;
}
if(gd.undoQueue === undefined ||
isNaN(gd.undoQueue.index) ||
gd.undoQueue.index <= 0) {
return;
}
// index is pointing to next *forward* queueObj, point to the one we're undoing
gd.undoQueue.index--;
// get the queueObj for instructions on how to undo
queueObj = gd.undoQueue.queue[gd.undoQueue.index];
// this sequence keeps things from adding to the queue during undo/redo
gd.undoQueue.inSequence = true;
for(i = 0; i < queueObj.undo.calls.length; i++) {
queue.plotDo(gd, queueObj.undo.calls[i], queueObj.undo.args[i]);
}
gd.undoQueue.inSequence = false;
gd.autoplay = false;
};
/**
* Redo the current object in the undo, then move forward in the queue.
*
* @param gd
*/
queue.redo = function redo(gd) {
var queueObj, i;
if(gd.framework && gd.framework.isPolar) {
gd.framework.redo();
return;
}
if(gd.undoQueue === undefined ||
isNaN(gd.undoQueue.index) ||
gd.undoQueue.index >= gd.undoQueue.queue.length) {
return;
}
// get the queueObj for instructions on how to undo
queueObj = gd.undoQueue.queue[gd.undoQueue.index];
// this sequence keeps things from adding to the queue during undo/redo
gd.undoQueue.inSequence = true;
for(i = 0; i < queueObj.redo.calls.length; i++) {
queue.plotDo(gd, queueObj.redo.calls[i], queueObj.redo.args[i]);
}
gd.undoQueue.inSequence = false;
gd.autoplay = false;
// index is pointing to the thing we just redid, move it
gd.undoQueue.index++;
};
/**
* Called by undo/redo to make the actual changes.
*
* Not meant to be called publically, but included for mocking out in tests.
*
* @param gd
* @param func
* @param args
*/
queue.plotDo = function(gd, func, args) {
gd.autoplay = true;
// this *won't* copy gd and it preserves `undefined` properties!
args = copyArgArray(gd, args);
// call the supplied function
func.apply(null, args);
};
module.exports = queue;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isArray = require('./is_array');
var isPlainObject = require('./is_plain_object');
/**
* Relink private _keys and keys with a function value from one container
* to the new container.
* Relink means copying if object is pass-by-value and adding a reference
* if object is pass-by-ref.
* This prevents deepCopying massive structures like a webgl context.
*/
module.exports = function relinkPrivateKeys(toContainer, fromContainer) {
var keys = Object.keys(fromContainer || {});
for(var i = 0; i < keys.length; i++) {
var k = keys[i],
fromVal = fromContainer[k],
toVal = toContainer[k];
if(k.charAt(0) === '_' || typeof fromVal === 'function') {
// if it already exists at this point, it's something
// that we recreate each time around, so ignore it
if(k in toContainer) continue;
toContainer[k] = fromVal;
}
else if(isArray(fromVal) && isArray(toVal) && isPlainObject(fromVal[0])) {
// recurse into arrays containers
for(var j = 0; j < fromVal.length; j++) {
if(isPlainObject(fromVal[j]) && isPlainObject(toVal[j])) {
relinkPrivateKeys(toVal[j], fromVal[j]);
}
}
}
else if(isPlainObject(fromVal) && isPlainObject(toVal)) {
// recurse into objects, but only if they still exist
relinkPrivateKeys(toVal, fromVal);
if(!Object.keys(toVal).length) delete toContainer[k];
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var loggers = require('./loggers');
/**
* findBin - find the bin for val - note that it can return outside the
* bin range any pos. or neg. integer for linear bins, or -1 or
* bins.length-1 for explicit.
* bins is either an object {start,size,end} or an array length #bins+1
* bins can be either increasing or decreasing but must be monotonic
* for linear bins, we can just calculate. For listed bins, run a binary
* search linelow (truthy) says the bin boundary should be attributed to
* the lower bin rather than the default upper bin
*/
exports.findBin = function(val, bins, linelow) {
if(isNumeric(bins.start)) {
return linelow ?
Math.ceil((val - bins.start) / bins.size) - 1 :
Math.floor((val - bins.start) / bins.size);
}
else {
var n1 = 0,
n2 = bins.length,
c = 0,
n,
test;
if(bins[bins.length - 1] >= bins[0]) {
test = linelow ? lessThan : lessOrEqual;
} else {
test = linelow ? greaterOrEqual : greaterThan;
}
// c is just to avoid infinite loops if there's an error
while(n1 < n2 && c++ < 100) {
n = Math.floor((n1 + n2) / 2);
if(test(bins[n], val)) n1 = n + 1;
else n2 = n;
}
if(c > 90) loggers.log('Long binary search...');
return n1 - 1;
}
};
function lessThan(a, b) { return a < b; }
function lessOrEqual(a, b) { return a <= b; }
function greaterThan(a, b) { return a > b; }
function greaterOrEqual(a, b) { return a >= b; }
exports.sorterAsc = function(a, b) { return a - b; };
exports.sorterDes = function(a, b) { return b - a; };
/**
* find distinct values in an array, lumping together ones that appear to
* just be off by a rounding error
* return the distinct values and the minimum difference between any two
*/
exports.distinctVals = function(valsIn) {
var vals = valsIn.slice(); // otherwise we sort the original array...
vals.sort(exports.sorterAsc);
var l = vals.length - 1,
minDiff = (vals[l] - vals[0]) || 1,
errDiff = minDiff / (l || 1) / 10000,
v2 = [vals[0]];
for(var i = 0; i < l; i++) {
// make sure values aren't just off by a rounding error
if(vals[i + 1] > vals[i] + errDiff) {
minDiff = Math.min(minDiff, vals[i + 1] - vals[i]);
v2.push(vals[i + 1]);
}
}
return {vals: v2, minDiff: minDiff};
};
/**
* return the smallest element from (sorted) array arrayIn that's bigger than val,
* or (reverse) the largest element smaller than val
* used to find the best tick given the minimum (non-rounded) tick
* particularly useful for date/time where things are not powers of 10
* binary search is probably overkill here...
*/
exports.roundUp = function(val, arrayIn, reverse) {
var low = 0,
high = arrayIn.length - 1,
mid,
c = 0,
dlow = reverse ? 0 : 1,
dhigh = reverse ? 1 : 0,
rounded = reverse ? Math.ceil : Math.floor;
// c is just to avoid infinite loops if there's an error
while(low < high && c++ < 100) {
mid = rounded((low + high) / 2);
if(arrayIn[mid] <= val) low = mid + dlow;
else high = mid - dhigh;
}
return arrayIn[low];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // works with our CSS cursor classes (see css/_cursor.scss) // to apply cursors to d3 single-element selections. // omit cursor to revert to the default. module.exports = function setCursor(el3, csr) { (el3.attr('class') || '').split(' ').forEach(function(cls) { if(cls.indexOf('cursor-') === 0) el3.classed(cls, false); }); if(csr) el3.classed('cursor-' + csr, true); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../components/color');
var noop = function() {};
/**
* Prints a no webgl error message into the scene container
* @param {scene instance} scene
*
* Expects 'scene' to have property 'container'
*
*/
module.exports = function showWebGlMsg(scene) {
for(var prop in scene) {
if(typeof scene[prop] === 'function') scene[prop] = noop;
}
scene.destroy = function() {
scene.container.parentNode.removeChild(scene.container);
};
var div = document.createElement('div');
div.textContent = 'Webgl is not supported by your browser - visit http://get.webgl.org for more info';
div.style.cursor = 'pointer';
div.style.fontSize = '24px';
div.style.color = Color.defaults[0];
scene.container.appendChild(div);
scene.container.style.background = '#FFFFFF';
scene.container.onclick = function() {
window.open('http://get.webgl.org');
};
// return before setting up camera and onrender methods
return false;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
/**
* aggNums() returns the result of an aggregate function applied to an array of
* values, where non-numerical values have been tossed out.
*
* @param {function} f - aggregation function (e.g., Math.min)
* @param {Number} v - initial value (continuing from previous calls)
* if there's no continuing value, use null for selector-type
* functions (max,min), or 0 for summations
* @param {Array} a - array to aggregate (may be nested, we will recurse,
* but all elements must have the same dimension)
* @param {Number} len - maximum length of a to aggregate
* @return {Number} - result of f applied to a starting from v
*/
exports.aggNums = function(f, v, a, len) {
var i,
b;
if(!len) len = a.length;
if(!isNumeric(v)) v = false;
if(Array.isArray(a[0])) {
b = new Array(len);
for(i = 0; i < len; i++) b[i] = exports.aggNums(f, v, a[i]);
a = b;
}
for(i = 0; i < len; i++) {
if(!isNumeric(v)) v = a[i];
else if(isNumeric(a[i])) v = f(+v, +a[i]);
}
return v;
};
/**
* mean & std dev functions using aggNums, so it handles non-numerics nicely
* even need to use aggNums instead of .length, to toss out non-numerics
*/
exports.len = function(data) {
return exports.aggNums(function(a) { return a + 1; }, 0, data);
};
exports.mean = function(data, len) {
if(!len) len = exports.len(data);
return exports.aggNums(function(a, b) { return a + b; }, 0, data) / len;
};
exports.variance = function(data, len, mean) {
if(!len) len = exports.len(data);
if(!isNumeric(mean)) mean = exports.mean(data, len);
return exports.aggNums(function(a, b) {
return a + Math.pow(b - mean, 2);
}, 0, data) / len;
};
exports.stdev = function(data, len, mean) {
return Math.sqrt(exports.variance(data, len, mean));
};
/**
* interp() computes a percentile (quantile) for a given distribution.
* We interpolate the distribution (to compute quantiles, we follow method #10 here:
* http://www.amstat.org/publications/jse/v14n3/langford.html).
* Typically the index or rank (n * arr.length) may be non-integer.
* For reference: ends are clipped to the extreme values in the array;
* For box plots: index you get is half a point too high (see
* http://en.wikipedia.org/wiki/Percentile#Nearest_rank) but note that this definition
* indexes from 1 rather than 0, so we subtract 1/2 (instead of add).
*
* @param {Array} arr - This array contains the values that make up the distribution.
* @param {Number} n - Between 0 and 1, n = p/100 is such that we compute the p^th percentile.
* For example, the 50th percentile (or median) corresponds to n = 0.5
* @return {Number} - percentile
*/
exports.interp = function(arr, n) {
if(!isNumeric(n)) throw 'n should be a finite number';
n = n * arr.length - 0.5;
if(n < 0) return arr[0];
if(n > arr.length - 1) return arr[arr.length - 1];
var frac = n % 1;
return frac * arr[Math.ceil(n)] + (1 - frac) * arr[Math.floor(n)];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var rgba = require('color-rgba');
function str2RgbaArray(color) {
var colorOut = rgba(color);
return colorOut.length ? colorOut : [0, 0, 0, 1];
}
module.exports = str2RgbaArray;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 1 6 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/* global MathJax:false */
var d3 = require('d3');
var Lib = require('../lib');
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
var stringMappings = require('../constants/string_mappings');
// Append SVG
d3.selection.prototype.appendSVG = function(_svgString) {
var skeleton = [
'<svg xmlns="', xmlnsNamespaces.svg, '" ',
'xmlns:xlink="', xmlnsNamespaces.xlink, '">',
_svgString,
'</svg>'
].join('');
var dom = new DOMParser().parseFromString(skeleton, 'application/xml'),
childNode = dom.documentElement.firstChild;
while(childNode) {
this.node().appendChild(this.node().ownerDocument.importNode(childNode, true));
childNode = childNode.nextSibling;
}
if(dom.querySelector('parsererror')) {
Lib.log(dom.querySelector('parsererror div').textContent);
return null;
}
return d3.select(this.node().lastChild);
};
// Text utilities
exports.html_entity_decode = function(s) {
var hiddenDiv = d3.select('body').append('div').style({display: 'none'}).html('');
var replaced = s.replace(/(&[^;]*;)/gi, function(d) {
if(d === '<') { return '<'; } // special handling for brackets
if(d === '&rt;') { return '>'; }
if(d.indexOf('<') !== -1 || d.indexOf('>') !== -1) { return ''; }
return hiddenDiv.html(d).text(); // everything else, let the browser decode it to unicode
});
hiddenDiv.remove();
return replaced;
};
exports.xml_entity_encode = function(str) {
return str.replace(/&(?!\w+;|\#[0-9]+;| \#x[0-9A-F]+;)/g, '&');
};
// text converter
function getSize(_selection, _dimension) {
return _selection.node().getBoundingClientRect()[_dimension];
}
exports.convertToTspans = function(_context, _callback) {
var str = _context.text();
var converted = convertToSVG(str);
var that = _context;
// Until we get tex integrated more fully (so it can be used along with non-tex)
// allow some elements to prohibit it by attaching 'data-notex' to the original
var tex = (!that.attr('data-notex')) && converted.match(/([^$]*)([$]+[^$]*[$]+)([^$]*)/);
var result = str;
var parent = d3.select(that.node().parentNode);
if(parent.empty()) return;
var svgClass = (that.attr('class')) ? that.attr('class').split(' ')[0] : 'text';
svgClass += '-math';
parent.selectAll('svg.' + svgClass).remove();
parent.selectAll('g.' + svgClass + '-group').remove();
_context.style({visibility: null});
for(var up = _context.node(); up && up.removeAttribute; up = up.parentNode) {
up.removeAttribute('data-bb');
}
function showText() {
if(!parent.empty()) {
svgClass = that.attr('class') + '-math';
parent.select('svg.' + svgClass).remove();
}
_context.text('')
.style({
visibility: 'inherit',
'white-space': 'pre'
});
result = _context.appendSVG(converted);
if(!result) _context.text(str);
if(_context.select('a').size()) {
// at least in Chrome, pointer-events does not seem
// to be honored in children of <text> elements
// so if we have an anchor, we have to make the
// whole element respond
_context.style('pointer-events', 'all');
}
if(_callback) _callback.call(that);
}
if(tex) {
var gd = Lib.getPlotDiv(that.node());
((gd && gd._promises) || []).push(new Promise(function(resolve) {
that.style({visibility: 'hidden'});
var config = {fontSize: parseInt(that.style('font-size'), 10)};
texToSVG(tex[2], config, function(_svgEl, _glyphDefs, _svgBBox) {
parent.selectAll('svg.' + svgClass).remove();
parent.selectAll('g.' + svgClass + '-group').remove();
var newSvg = _svgEl && _svgEl.select('svg');
if(!newSvg || !newSvg.node()) {
showText();
resolve();
return;
}
var mathjaxGroup = parent.append('g')
.classed(svgClass + '-group', true)
.attr({'pointer-events': 'none'});
mathjaxGroup.node().appendChild(newSvg.node());
// stitch the glyph defs
if(_glyphDefs && _glyphDefs.node()) {
newSvg.node().insertBefore(_glyphDefs.node().cloneNode(true),
newSvg.node().firstChild);
}
newSvg.attr({
'class': svgClass,
height: _svgBBox.height,
preserveAspectRatio: 'xMinYMin meet'
})
.style({overflow: 'visible', 'pointer-events': 'none'});
var fill = that.style('fill') || 'black';
newSvg.select('g').attr({fill: fill, stroke: fill});
var newSvgW = getSize(newSvg, 'width'),
newSvgH = getSize(newSvg, 'height'),
newX = +that.attr('x') - newSvgW *
{start: 0, middle: 0.5, end: 1}[that.attr('text-anchor') || 'start'],
// font baseline is about 1/4 fontSize below centerline
textHeight = parseInt(that.style('font-size'), 10) ||
getSize(that, 'height'),
dy = -textHeight / 4;
if(svgClass[0] === 'y') {
mathjaxGroup.attr({
transform: 'rotate(' + [-90, +that.attr('x'), +that.attr('y')] +
') translate(' + [-newSvgW / 2, dy - newSvgH / 2] + ')'
});
newSvg.attr({x: +that.attr('x'), y: +that.attr('y')});
}
else if(svgClass[0] === 'l') {
newSvg.attr({x: that.attr('x'), y: dy - (newSvgH / 2)});
}
else if(svgClass[0] === 'a') {
newSvg.attr({x: 0, y: dy});
}
else {
newSvg.attr({x: newX, y: (+that.attr('y') + dy - newSvgH / 2)});
}
if(_callback) _callback.call(that, mathjaxGroup);
resolve(mathjaxGroup);
});
}));
}
else showText();
return _context;
};
// MathJax
function cleanEscapesForTex(s) {
return s.replace(/(<|<|<)/g, '\\lt ')
.replace(/(>|>|>)/g, '\\gt ');
}
function texToSVG(_texString, _config, _callback) {
var randomID = 'math-output-' + Lib.randstr([], 64);
var tmpDiv = d3.select('body').append('div')
.attr({id: randomID})
.style({visibility: 'hidden', position: 'absolute'})
.style({'font-size': _config.fontSize + 'px'})
.text(cleanEscapesForTex(_texString));
MathJax.Hub.Queue(['Typeset', MathJax.Hub, tmpDiv.node()], function() {
var glyphDefs = d3.select('body').select('#MathJax_SVG_glyphs');
if(tmpDiv.select('.MathJax_SVG').empty() || !tmpDiv.select('svg').node()) {
Lib.log('There was an error in the tex syntax.', _texString);
_callback();
}
else {
var svgBBox = tmpDiv.select('svg').node().getBoundingClientRect();
_callback(tmpDiv.select('.MathJax_SVG'), glyphDefs, svgBBox);
}
tmpDiv.remove();
});
}
var TAG_STYLES = {
// would like to use baseline-shift but FF doesn't support it yet
// so we need to use dy along with the uber hacky shift-back-to
// baseline below
sup: 'font-size:70%" dy="-0.6em',
sub: 'font-size:70%" dy="0.3em',
b: 'font-weight:bold',
i: 'font-style:italic',
a: '',
span: '',
br: '',
em: 'font-style:italic;font-weight:bold'
};
var PROTOCOLS = ['http:', 'https:', 'mailto:'];
var STRIP_TAGS = new RegExp('</?(' + Object.keys(TAG_STYLES).join('|') + ')( [^>]*)?/?>', 'g');
var ENTITY_TO_UNICODE = Object.keys(stringMappings.entityToUnicode).map(function(k) {
return {
regExp: new RegExp('&' + k + ';', 'g'),
sub: stringMappings.entityToUnicode[k]
};
});
var UNICODE_TO_ENTITY = Object.keys(stringMappings.unicodeToEntity).map(function(k) {
return {
regExp: new RegExp(k, 'g'),
sub: '&' + stringMappings.unicodeToEntity[k] + ';'
};
});
var NEWLINES = /(\r\n?|\n)/g;
exports.plainText = function(_str) {
// strip out our pseudo-html so we have a readable
// version to put into text fields
return (_str || '').replace(STRIP_TAGS, ' ');
};
function replaceFromMapObject(_str, list) {
var out = _str || '';
for(var i = 0; i < list.length; i++) {
var item = list[i];
out = out.replace(item.regExp, item.sub);
}
return out;
}
function convertEntities(_str) {
return replaceFromMapObject(_str, ENTITY_TO_UNICODE);
}
function encodeForHTML(_str) {
return replaceFromMapObject(_str, UNICODE_TO_ENTITY);
}
function convertToSVG(_str) {
_str = convertEntities(_str);
// normalize behavior between IE and others wrt newlines and whitespace:pre
// this combination makes IE barf https://github.com/plotly/plotly.js/issues/746
// Chrome and FF display \n, \r, or \r\n as a space in this mode.
// I feel like at some point we turned these into <br> but currently we don't so
// I'm just going to cement what we do now in Chrome and FF
_str = _str.replace(NEWLINES, ' ');
var result = _str
.split(/(<[^<>]*>)/).map(function(d) {
var match = d.match(/<(\/?)([^ >]*)\s*(.*)>/i),
tag = match && match[2].toLowerCase(),
style = TAG_STYLES[tag];
if(style !== undefined) {
var close = match[1],
extra = match[3],
/**
* extraStyle: any random extra css (that's supported by svg)
* use this like <span style="font-family:Arial"> to change font in the middle
*
* at one point we supported <font family="..." size="..."> but as this isn't even
* valid HTML anymore and we dropped it accidentally for many months, we will not
* resurrect it.
*/
extraStyle = extra.match(/^style\s*=\s*"([^"]+)"\s*/i);
// anchor and br are the only ones that don't turn into a tspan
if(tag === 'a') {
if(close) return '</a>';
else if(extra.substr(0, 4).toLowerCase() !== 'href') return '<a>';
else {
// remove quotes, leading '=', replace '&' with '&'
var href = extra.substr(4)
.replace(/["']/g, '')
.replace(/=/, '');
// check protocol
var dummyAnchor = document.createElement('a');
dummyAnchor.href = href;
if(PROTOCOLS.indexOf(dummyAnchor.protocol) === -1) return '<a>';
return '<a xlink:show="new" xlink:href="' + encodeForHTML(href) + '">';
}
}
else if(tag === 'br') return '<br>';
else if(close) {
// closing tag
// sub/sup: extra tspan with zero-width space to get back to the right baseline
if(tag === 'sup') return '</tspan><tspan dy="0.42em">​</tspan>';
if(tag === 'sub') return '</tspan><tspan dy="-0.21em">​</tspan>';
else return '</tspan>';
}
else {
var tspanStart = '<tspan';
if(tag === 'sup' || tag === 'sub') {
// sub/sup: extra zero-width space, fixes problem if new line starts with sub/sup
tspanStart = '​' + tspanStart;
}
if(extraStyle) {
// most of the svg css users will care about is just like html,
// but font color is different. Let our users ignore this.
extraStyle = extraStyle[1].replace(/(^|;)\s*color:/, '$1 fill:');
style = encodeForHTML(extraStyle) + (style ? ';' + style : '');
}
return tspanStart + (style ? ' style="' + style + '"' : '') + '>';
}
}
else {
return exports.xml_entity_encode(d).replace(/</g, '<');
}
});
var indices = [];
for(var index = result.indexOf('<br>'); index > 0; index = result.indexOf('<br>', index + 1)) {
indices.push(index);
}
var count = 0;
indices.forEach(function(d) {
var brIndex = d + count;
var search = result.slice(0, brIndex);
var previousOpenTag = '';
for(var i2 = search.length - 1; i2 >= 0; i2--) {
var isTag = search[i2].match(/<(\/?).*>/i);
if(isTag && search[i2] !== '<br>') {
if(!isTag[1]) previousOpenTag = search[i2];
break;
}
}
if(previousOpenTag) {
result.splice(brIndex + 1, 0, previousOpenTag);
result.splice(brIndex, 0, '</tspan>');
count += 2;
}
});
var joined = result.join('');
var splitted = joined.split(/<br>/gi);
if(splitted.length > 1) {
result = splitted.map(function(d, i) {
// TODO: figure out max font size of this line and alter dy
// this requires either:
// 1) bringing the base font size into convertToTspans, or
// 2) only allowing relative percentage font sizes.
// I think #2 is the way to go
return '<tspan class="line" dy="' + (i * 1.3) + 'em">' + d + '</tspan>';
});
}
return result.join('');
}
function alignHTMLWith(_base, container, options) {
var alignH = options.horizontalAlign,
alignV = options.verticalAlign || 'top',
bRect = _base.node().getBoundingClientRect(),
cRect = container.node().getBoundingClientRect(),
thisRect,
getTop,
getLeft;
if(alignV === 'bottom') {
getTop = function() { return bRect.bottom - thisRect.height; };
} else if(alignV === 'middle') {
getTop = function() { return bRect.top + (bRect.height - thisRect.height) / 2; };
} else { // default: top
getTop = function() { return bRect.top; };
}
if(alignH === 'right') {
getLeft = function() { return bRect.right - thisRect.width; };
} else if(alignH === 'center') {
getLeft = function() { return bRect.left + (bRect.width - thisRect.width) / 2; };
} else { // default: left
getLeft = function() { return bRect.left; };
}
return function() {
thisRect = this.node().getBoundingClientRect();
this.style({
top: (getTop() - cRect.top) + 'px',
left: (getLeft() - cRect.left) + 'px',
'z-index': 1000
});
return this;
};
}
// Editable title
exports.makeEditable = function(context, _delegate, options) {
if(!options) options = {};
var that = this;
var dispatch = d3.dispatch('edit', 'input', 'cancel');
var textSelection = d3.select(this.node())
.style({'pointer-events': 'all'});
var handlerElement = _delegate || textSelection;
if(_delegate) textSelection.style({'pointer-events': 'none'});
function handleClick() {
appendEditable();
that.style({opacity: 0});
// also hide any mathjax svg
var svgClass = handlerElement.attr('class'),
mathjaxClass;
if(svgClass) mathjaxClass = '.' + svgClass.split(' ')[0] + '-math-group';
else mathjaxClass = '[class*=-math-group]';
if(mathjaxClass) {
d3.select(that.node().parentNode).select(mathjaxClass).style({opacity: 0});
}
}
function selectElementContents(_el) {
var el = _el.node();
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
el.focus();
}
function appendEditable() {
var gd = Lib.getPlotDiv(that.node()),
plotDiv = d3.select(gd),
container = plotDiv.select('.svg-container'),
div = container.append('div');
div.classed('plugin-editable editable', true)
.style({
position: 'absolute',
'font-family': that.style('font-family') || 'Arial',
'font-size': that.style('font-size') || 12,
color: options.fill || that.style('fill') || 'black',
opacity: 1,
'background-color': options.background || 'transparent',
outline: '#ffffff33 1px solid',
margin: [-parseFloat(that.style('font-size')) / 8 + 1, 0, 0, -1].join('px ') + 'px',
padding: '0',
'box-sizing': 'border-box'
})
.attr({contenteditable: true})
.text(options.text || that.attr('data-unformatted'))
.call(alignHTMLWith(that, container, options))
.on('blur', function() {
gd._editing = false;
that.text(this.textContent)
.style({opacity: 1});
var svgClass = d3.select(this).attr('class'),
mathjaxClass;
if(svgClass) mathjaxClass = '.' + svgClass.split(' ')[0] + '-math-group';
else mathjaxClass = '[class*=-math-group]';
if(mathjaxClass) {
d3.select(that.node().parentNode).select(mathjaxClass).style({opacity: 0});
}
var text = this.textContent;
d3.select(this).transition().duration(0).remove();
d3.select(document).on('mouseup', null);
dispatch.edit.call(that, text);
})
.on('focus', function() {
var context = this;
gd._editing = true;
d3.select(document).on('mouseup', function() {
if(d3.event.target === context) return false;
if(document.activeElement === div.node()) div.node().blur();
});
})
.on('keyup', function() {
if(d3.event.which === 27) {
gd._editing = false;
that.style({opacity: 1});
d3.select(this)
.style({opacity: 0})
.on('blur', function() { return false; })
.transition().remove();
dispatch.cancel.call(that, this.textContent);
}
else {
dispatch.input.call(that, this.textContent);
d3.select(this).call(alignHTMLWith(that, container, options));
}
})
.on('keydown', function() {
if(d3.event.which === 13) this.blur();
})
.call(selectElementContents);
}
if(options.immediate) handleClick();
else handlerElement.on('click', handleClick);
return d3.rebind(this, dispatch, 'on');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
/**
* convert a linear value into a logged value, folding negative numbers into
* the given range
*/
module.exports = function toLogRange(val, range) {
if(val > 0) return Math.log(val) / Math.LN10;
// move a negative value reference to a log axis - just put the
// result at the lowest range value on the plot (or if the range also went negative,
// one millionth of the top of the range)
var newVal = Math.log(Math.min(range[0], range[1])) / Math.LN10;
if(!isNumeric(newVal)) newVal = Math.log(Math.max(range[0], range[1])) / Math.LN10 - 6;
return newVal;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var topojsonUtils = module.exports = {};
var locationmodeToLayer = require('../plots/geo/constants').locationmodeToLayer;
var topojsonFeature = require('topojson-client').feature;
topojsonUtils.getTopojsonName = function(geoLayout) {
return [
geoLayout.scope.replace(/ /g, '-'), '_',
geoLayout.resolution.toString(), 'm'
].join('');
};
topojsonUtils.getTopojsonPath = function(topojsonURL, topojsonName) {
return topojsonURL + topojsonName + '.json';
};
topojsonUtils.getTopojsonFeatures = function(trace, topojson) {
var layer = locationmodeToLayer[trace.locationmode],
obj = topojson.objects[layer];
return topojsonFeature(topojson, obj).features;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; function truncateFloat32(arrayIn, len) { var arrayOut = new Float32Array(len); for(var i = 0; i < len; i++) arrayOut[i] = arrayIn[i]; return arrayOut; } function truncateFloat64(arrayIn, len) { var arrayOut = new Float64Array(len); for(var i = 0; i < len; i++) arrayOut[i] = arrayIn[i]; return arrayOut; } /** * Truncate a typed array to some length. * For some reason, ES2015 Float32Array.prototype.slice takes * 2x as long, therefore we aren't checking for its existence */ module.exports = function truncate(arrayIn, len) { if(arrayIn instanceof Float32Array) return truncateFloat32(arrayIn, len); if(arrayIn instanceof Float64Array) return truncateFloat64(arrayIn, len); throw new Error('This array type is not yet supported by `truncate`.'); }; |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| container_array_match.js | 10.53% | (2 / 19) | 0% | (0 / 14) | 0% | (0 / 1) | 13.33% | (2 / 15) | |
| helpers.js | 7.3% | (20 / 274) | 0% | (0 / 240) | 0% | (0 / 15) | 8.4% | (20 / 238) | |
| manage_arrays.js | 11.11% | (9 / 81) | 0% | (0 / 54) | 0% | (0 / 3) | 12.86% | (9 / 70) | |
| plot_api.js | 6.1% | (74 / 1213) | 0% | (0 / 862) | 0% | (0 / 84) | 6.63% | (74 / 1116) | |
| plot_config.js | 33.33% | (2 / 6) | 0% | (0 / 2) | 0% | (0 / 1) | 33.33% | (2 / 6) | |
| plot_schema.js | 13.41% | (22 / 164) | 0% | (0 / 65) | 0% | (0 / 29) | 14.19% | (22 / 155) | |
| register.js | 14.63% | (6 / 41) | 0% | (0 / 30) | 0% | (0 / 4) | 14.63% | (6 / 41) | |
| set_plot_config.js | 75% | (3 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 75% | (3 / 4) | |
| subroutines.js | 13.51% | (20 / 148) | 0% | (0 / 130) | 0% | (0 / 15) | 13.89% | (20 / 144) | |
| to_image.js | 23.81% | (10 / 42) | 0% | (0 / 18) | 0% | (0 / 10) | 24.39% | (10 / 41) | |
| validate.js | 7.19% | (11 / 153) | 0% | (0 / 96) | 0% | (0 / 15) | 7.48% | (11 / 147) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../registry');
/*
* containerArrayMatch: does this attribute string point into a
* layout container array?
*
* @param {String} astr: an attribute string, like *annotations[2].text*
*
* @returns {Object | false} Returns false if `astr` doesn't match a container
* array. If it does, returns:
* {array: {String}, index: {Number}, property: {String}}
* ie the attribute string for the array, the index within the array (or ''
* if the whole array) and the property within that (or '' if the whole array
* or the whole object)
*/
module.exports = function containerArrayMatch(astr) {
var rootContainers = Registry.layoutArrayContainers,
regexpContainers = Registry.layoutArrayRegexes,
rootPart = astr.split('[')[0],
arrayStr,
match;
// look for regexp matches first, because they may be nested inside root matches
// eg updatemenus[i].buttons is nested inside updatemenus
for(var i = 0; i < regexpContainers.length; i++) {
match = astr.match(regexpContainers[i]);
if(match && match.index === 0) {
arrayStr = match[0];
break;
}
}
// now look for root matches
if(!arrayStr) arrayStr = rootContainers[rootContainers.indexOf(rootPart)];
if(!arrayStr) return false;
var tail = astr.substr(arrayStr.length);
if(!tail) return {array: arrayStr, index: '', property: ''};
match = tail.match(/^\[(0|[1-9][0-9]*)\](\.(.+))?$/);
if(!match) return false;
return {array: arrayStr, index: Number(match[1]), property: match[3] || ''};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var m4FromQuat = require('gl-mat4/fromQuat');
var Registry = require('../registry');
var Lib = require('../lib');
var Plots = require('../plots/plots');
var Axes = require('../plots/cartesian/axes');
var Color = require('../components/color');
// Get the container div: we store all variables for this plot as
// properties of this div
// some callers send this in by DOM element, others by id (string)
exports.getGraphDiv = function(gd) {
var gdElement;
if(typeof gd === 'string') {
gdElement = document.getElementById(gd);
if(gdElement === null) {
throw new Error('No DOM element with id \'' + gd + '\' exists on the page.');
}
return gdElement;
}
else if(gd === null || gd === undefined) {
throw new Error('DOM element provided is null or undefined');
}
return gd; // otherwise assume that gd is a DOM element
};
// clear the promise queue if one of them got rejected
exports.clearPromiseQueue = function(gd) {
if(Array.isArray(gd._promises) && gd._promises.length > 0) {
Lib.log('Clearing previous rejected promises from queue.');
}
gd._promises = [];
};
// make a few changes to the layout right away
// before it gets used for anything
// backward compatibility and cleanup of nonstandard options
exports.cleanLayout = function(layout) {
var i, j;
if(!layout) layout = {};
// cannot have (x|y)axis1, numbering goes axis, axis2, axis3...
if(layout.xaxis1) {
if(!layout.xaxis) layout.xaxis = layout.xaxis1;
delete layout.xaxis1;
}
if(layout.yaxis1) {
if(!layout.yaxis) layout.yaxis = layout.yaxis1;
delete layout.yaxis1;
}
var axList = Axes.list({_fullLayout: layout});
for(i = 0; i < axList.length; i++) {
var ax = axList[i];
if(ax.anchor && ax.anchor !== 'free') {
ax.anchor = Axes.cleanId(ax.anchor);
}
if(ax.overlaying) ax.overlaying = Axes.cleanId(ax.overlaying);
// old method of axis type - isdate and islog (before category existed)
if(!ax.type) {
if(ax.isdate) ax.type = 'date';
else if(ax.islog) ax.type = 'log';
else if(ax.isdate === false && ax.islog === false) ax.type = 'linear';
}
if(ax.autorange === 'withzero' || ax.autorange === 'tozero') {
ax.autorange = true;
ax.rangemode = 'tozero';
}
delete ax.islog;
delete ax.isdate;
delete ax.categories; // replaced by _categories
// prune empty domain arrays made before the new nestedProperty
if(emptyContainer(ax, 'domain')) delete ax.domain;
// autotick -> tickmode
if(ax.autotick !== undefined) {
if(ax.tickmode === undefined) {
ax.tickmode = ax.autotick ? 'auto' : 'linear';
}
delete ax.autotick;
}
}
var annotationsLen = Array.isArray(layout.annotations) ? layout.annotations.length : 0;
for(i = 0; i < annotationsLen; i++) {
var ann = layout.annotations[i];
if(!Lib.isPlainObject(ann)) continue;
if(ann.ref) {
if(ann.ref === 'paper') {
ann.xref = 'paper';
ann.yref = 'paper';
}
else if(ann.ref === 'data') {
ann.xref = 'x';
ann.yref = 'y';
}
delete ann.ref;
}
cleanAxRef(ann, 'xref');
cleanAxRef(ann, 'yref');
}
var shapesLen = Array.isArray(layout.shapes) ? layout.shapes.length : 0;
for(i = 0; i < shapesLen; i++) {
var shape = layout.shapes[i];
if(!Lib.isPlainObject(shape)) continue;
cleanAxRef(shape, 'xref');
cleanAxRef(shape, 'yref');
}
var legend = layout.legend;
if(legend) {
// check for old-style legend positioning (x or y is +/- 100)
if(legend.x > 3) {
legend.x = 1.02;
legend.xanchor = 'left';
}
else if(legend.x < -2) {
legend.x = -0.02;
legend.xanchor = 'right';
}
if(legend.y > 3) {
legend.y = 1.02;
legend.yanchor = 'bottom';
}
else if(legend.y < -2) {
legend.y = -0.02;
legend.yanchor = 'top';
}
}
/*
* Moved from rotate -> orbit for dragmode
*/
if(layout.dragmode === 'rotate') layout.dragmode = 'orbit';
// cannot have scene1, numbering goes scene, scene2, scene3...
if(layout.scene1) {
if(!layout.scene) layout.scene = layout.scene1;
delete layout.scene1;
}
/*
* Clean up Scene layouts
*/
var sceneIds = Plots.getSubplotIds(layout, 'gl3d');
for(i = 0; i < sceneIds.length; i++) {
var scene = layout[sceneIds[i]];
// clean old Camera coords
var cameraposition = scene.cameraposition;
if(Array.isArray(cameraposition) && cameraposition[0].length === 4) {
var rotation = cameraposition[0],
center = cameraposition[1],
radius = cameraposition[2],
mat = m4FromQuat([], rotation),
eye = [];
for(j = 0; j < 3; ++j) {
eye[j] = center[i] + radius * mat[2 + 4 * j];
}
scene.camera = {
eye: {x: eye[0], y: eye[1], z: eye[2]},
center: {x: center[0], y: center[1], z: center[2]},
up: {x: mat[1], y: mat[5], z: mat[9]}
};
delete scene.cameraposition;
}
}
// sanitize rgb(fractions) and rgba(fractions) that old tinycolor
// supported, but new tinycolor does not because they're not valid css
Color.clean(layout);
return layout;
};
function cleanAxRef(container, attr) {
var valIn = container[attr],
axLetter = attr.charAt(0);
if(valIn && valIn !== 'paper') {
container[attr] = Axes.cleanId(valIn, axLetter);
}
}
// Make a few changes to the data right away
// before it gets used for anything
exports.cleanData = function(data, existingData) {
// Enforce unique IDs
var suids = [], // seen uids --- so we can weed out incoming repeats
uids = data.concat(Array.isArray(existingData) ? existingData : [])
.filter(function(trace) { return 'uid' in trace; })
.map(function(trace) { return trace.uid; });
for(var tracei = 0; tracei < data.length; tracei++) {
var trace = data[tracei];
var i;
// assign uids to each trace and detect collisions.
if(!('uid' in trace) || suids.indexOf(trace.uid) !== -1) {
var newUid;
for(i = 0; i < 100; i++) {
newUid = Lib.randstr(uids);
if(suids.indexOf(newUid) === -1) break;
}
trace.uid = Lib.randstr(uids);
uids.push(trace.uid);
}
// keep track of already seen uids, so that if there are
// doubles we force the trace with a repeat uid to
// acquire a new one
suids.push(trace.uid);
// BACKWARD COMPATIBILITY FIXES
// use xbins to bin data in x, and ybins to bin data in y
if(trace.type === 'histogramy' && 'xbins' in trace && !('ybins' in trace)) {
trace.ybins = trace.xbins;
delete trace.xbins;
}
// error_y.opacity is obsolete - merge into color
if(trace.error_y && 'opacity' in trace.error_y) {
var dc = Color.defaults,
yeColor = trace.error_y.color ||
(Registry.traceIs(trace, 'bar') ? Color.defaultLine : dc[tracei % dc.length]);
trace.error_y.color = Color.addOpacity(
Color.rgb(yeColor),
Color.opacity(yeColor) * trace.error_y.opacity);
delete trace.error_y.opacity;
}
// convert bardir to orientation, and put the data into
// the axes it's eventually going to be used with
if('bardir' in trace) {
if(trace.bardir === 'h' && (Registry.traceIs(trace, 'bar') ||
trace.type.substr(0, 9) === 'histogram')) {
trace.orientation = 'h';
exports.swapXYData(trace);
}
delete trace.bardir;
}
// now we have only one 1D histogram type, and whether
// it uses x or y data depends on trace.orientation
if(trace.type === 'histogramy') exports.swapXYData(trace);
if(trace.type === 'histogramx' || trace.type === 'histogramy') {
trace.type = 'histogram';
}
// scl->scale, reversescl->reversescale
if('scl' in trace) {
trace.colorscale = trace.scl;
delete trace.scl;
}
if('reversescl' in trace) {
trace.reversescale = trace.reversescl;
delete trace.reversescl;
}
// axis ids x1 -> x, y1-> y
if(trace.xaxis) trace.xaxis = Axes.cleanId(trace.xaxis, 'x');
if(trace.yaxis) trace.yaxis = Axes.cleanId(trace.yaxis, 'y');
// scene ids scene1 -> scene
if(Registry.traceIs(trace, 'gl3d') && trace.scene) {
trace.scene = Plots.subplotsRegistry.gl3d.cleanId(trace.scene);
}
if(!Registry.traceIs(trace, 'pie') && !Registry.traceIs(trace, 'bar')) {
if(Array.isArray(trace.textposition)) {
trace.textposition = trace.textposition.map(cleanTextPosition);
}
else if(trace.textposition) {
trace.textposition = cleanTextPosition(trace.textposition);
}
}
// fix typo in colorscale definition
if(Registry.traceIs(trace, '2dMap')) {
if(trace.colorscale === 'YIGnBu') trace.colorscale = 'YlGnBu';
if(trace.colorscale === 'YIOrRd') trace.colorscale = 'YlOrRd';
}
if(Registry.traceIs(trace, 'markerColorscale') && trace.marker) {
var cont = trace.marker;
if(cont.colorscale === 'YIGnBu') cont.colorscale = 'YlGnBu';
if(cont.colorscale === 'YIOrRd') cont.colorscale = 'YlOrRd';
}
// fix typo in surface 'highlight*' definitions
if(trace.type === 'surface' && Lib.isPlainObject(trace.contours)) {
var dims = ['x', 'y', 'z'];
for(i = 0; i < dims.length; i++) {
var opts = trace.contours[dims[i]];
if(!Lib.isPlainObject(opts)) continue;
if(opts.highlightColor) {
opts.highlightcolor = opts.highlightColor;
delete opts.highlightColor;
}
if(opts.highlightWidth) {
opts.highlightwidth = opts.highlightWidth;
delete opts.highlightWidth;
}
}
}
// transforms backward compatibility fixes
if(Array.isArray(trace.transforms)) {
var transforms = trace.transforms;
for(i = 0; i < transforms.length; i++) {
var transform = transforms[i];
if(!Lib.isPlainObject(transform)) continue;
if(transform.type === 'filter') {
if(transform.filtersrc) {
transform.target = transform.filtersrc;
delete transform.filtersrc;
}
if(transform.calendar) {
if(!transform.valuecalendar) {
transform.valuecalendar = transform.calendar;
}
delete transform.calendar;
}
}
}
}
// prune empty containers made before the new nestedProperty
if(emptyContainer(trace, 'line')) delete trace.line;
if('marker' in trace) {
if(emptyContainer(trace.marker, 'line')) delete trace.marker.line;
if(emptyContainer(trace, 'marker')) delete trace.marker;
}
// sanitize rgb(fractions) and rgba(fractions) that old tinycolor
// supported, but new tinycolor does not because they're not valid css
Color.clean(trace);
}
};
// textposition - support partial attributes (ie just 'top')
// and incorrect use of middle / center etc.
function cleanTextPosition(textposition) {
var posY = 'middle',
posX = 'center';
if(textposition.indexOf('top') !== -1) posY = 'top';
else if(textposition.indexOf('bottom') !== -1) posY = 'bottom';
if(textposition.indexOf('left') !== -1) posX = 'left';
else if(textposition.indexOf('right') !== -1) posX = 'right';
return posY + ' ' + posX;
}
function emptyContainer(outer, innerStr) {
return (innerStr in outer) &&
(typeof outer[innerStr] === 'object') &&
(Object.keys(outer[innerStr]).length === 0);
}
// swap all the data and data attributes associated with x and y
exports.swapXYData = function(trace) {
var i;
Lib.swapAttrs(trace, ['?', '?0', 'd?', '?bins', 'nbins?', 'autobin?', '?src', 'error_?']);
if(Array.isArray(trace.z) && Array.isArray(trace.z[0])) {
if(trace.transpose) delete trace.transpose;
else trace.transpose = true;
}
if(trace.error_x && trace.error_y) {
var errorY = trace.error_y,
copyYstyle = ('copy_ystyle' in errorY) ? errorY.copy_ystyle :
!(errorY.color || errorY.thickness || errorY.width);
Lib.swapAttrs(trace, ['error_?.copy_ystyle']);
if(copyYstyle) {
Lib.swapAttrs(trace, ['error_?.color', 'error_?.thickness', 'error_?.width']);
}
}
if(trace.hoverinfo) {
var hoverInfoParts = trace.hoverinfo.split('+');
for(i = 0; i < hoverInfoParts.length; i++) {
if(hoverInfoParts[i] === 'x') hoverInfoParts[i] = 'y';
else if(hoverInfoParts[i] === 'y') hoverInfoParts[i] = 'x';
}
trace.hoverinfo = hoverInfoParts.join('+');
}
};
// coerce traceIndices input to array of trace indices
exports.coerceTraceIndices = function(gd, traceIndices) {
if(isNumeric(traceIndices)) {
return [traceIndices];
}
else if(!Array.isArray(traceIndices) || !traceIndices.length) {
return gd.data.map(function(_, i) { return i; });
}
return traceIndices;
};
/**
* Manages logic around array container item creation / deletion / update
* that nested property alone can't handle.
*
* @param {Object} np
* nested property of update attribute string about trace or layout object
* @param {*} newVal
* update value passed to restyle / relayout / update
* @param {Object} undoit
* undo hash (N.B. undoit may be mutated here).
*
*/
exports.manageArrayContainers = function(np, newVal, undoit) {
var obj = np.obj,
parts = np.parts,
pLength = parts.length,
pLast = parts[pLength - 1];
var pLastIsNumber = isNumeric(pLast);
// delete item
if(pLastIsNumber && newVal === null) {
// Clear item in array container when new value is null
var contPath = parts.slice(0, pLength - 1).join('.'),
cont = Lib.nestedProperty(obj, contPath).get();
cont.splice(pLast, 1);
// Note that nested property clears null / undefined at end of
// array container, but not within them.
}
// create item
else if(pLastIsNumber && np.get() === undefined) {
// When adding a new item, make sure undo command will remove it
if(np.get() === undefined) undoit[np.astr] = null;
np.set(newVal);
}
// update item
else {
// If the last part of attribute string isn't a number,
// np.set is all we need.
np.set(newVal);
}
};
/*
* Match the part to strip off to turn an attribute into its parent
* really it should be either '.some_characters' or '[number]'
* but we're a little more permissive here and match either
* '.not_brackets_or_dot' or '[not_brackets_or_dot]'
*/
var ATTR_TAIL_RE = /(\.[^\[\]\.]+|\[[^\[\]\.]+\])$/;
function getParent(attr) {
var tail = attr.search(ATTR_TAIL_RE);
if(tail > 0) return attr.substr(0, tail);
}
/*
* hasParent: does an attribute object contain a parent of the given attribute?
* for example, given 'images[2].x' do we also have 'images' or 'images[2]'?
*
* @param {Object} aobj
* update object, whose keys are attribute strings and values are their new settings
* @param {string} attr
* the attribute string to test against
* @returns {Boolean}
* is a parent of attr present in aobj?
*/
exports.hasParent = function(aobj, attr) {
var attrParent = getParent(attr);
while(attrParent) {
if(attrParent in aobj) return true;
attrParent = getParent(attrParent);
}
return false;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var nestedProperty = require('../lib/nested_property');
var isPlainObject = require('../lib/is_plain_object');
var noop = require('../lib/noop');
var Loggers = require('../lib/loggers');
var Registry = require('../registry');
exports.containerArrayMatch = require('./container_array_match');
var isAddVal = exports.isAddVal = function isAddVal(val) {
return val === 'add' || isPlainObject(val);
};
var isRemoveVal = exports.isRemoveVal = function isRemoveVal(val) {
return val === null || val === 'remove';
};
/*
* applyContainerArrayChanges: for managing arrays of layout components in relayout
* handles them all with a consistent interface.
*
* Here are the supported actions -> relayout calls -> edits we get here
* (as prepared in _relayout):
*
* add an empty obj -> {'annotations[2]': 'add'} -> {2: {'': 'add'}}
* add a specific obj -> {'annotations[2]': {attrs}} -> {2: {'': {attrs}}}
* delete an obj -> {'annotations[2]': 'remove'} -> {2: {'': 'remove'}}
* -> {'annotations[2]': null} -> {2: {'': null}}
* delete the whole array -> {'annotations': 'remove'} -> {'': {'': 'remove'}}
* -> {'annotations': null} -> {'': {'': null}}
* edit an object -> {'annotations[2].text': 'boo'} -> {2: {'text': 'boo'}}
*
* You can combine many edits to different objects. Objects are added and edited
* in ascending order, then removed in descending order.
* For example, starting with [a, b, c], if you want to:
* - replace b with d:
* {'annotations[1]': d, 'annotations[2]': null} (b is item 2 after adding d)
* - add a new item d between a and b, and edit b:
* {'annotations[1]': d, 'annotations[2].x': newX} (b is item 2 after adding d)
* - delete b and edit c:
* {'annotations[1]': null, 'annotations[2].x': newX} (c is edited before b is removed)
*
* You CANNOT combine adding/deleting an item at index `i` with edits to the same index `i`
* You CANNOT combine replacing/deleting the whole array with anything else (for the same array).
*
* @param {HTMLDivElement} gd
* the DOM element of the graph container div
* @param {Lib.nestedProperty} componentType: the array we are editing
* @param {Object} edits
* the changes to make; keys are indices to edit, values are themselves objects:
* {attr: newValue} of changes to make to that index (with add/remove behavior
* in special values of the empty attr)
* @param {Object} flags
* the flags for which actions we're going to perform to display these (and
* any other) changes. If we're already `recalc`ing, we don't need to redraw
* individual items
*
* @returns {bool} `true` if it managed to complete drawing of the changes
* `false` would mean the parent should replot.
*/
exports.applyContainerArrayChanges = function applyContainerArrayChanges(gd, np, edits, flags) {
var componentType = np.astr,
supplyComponentDefaults = Registry.getComponentMethod(componentType, 'supplyLayoutDefaults'),
draw = Registry.getComponentMethod(componentType, 'draw'),
drawOne = Registry.getComponentMethod(componentType, 'drawOne'),
replotLater = flags.replot || flags.recalc || (supplyComponentDefaults === noop) ||
(draw === noop),
layout = gd.layout,
fullLayout = gd._fullLayout;
if(edits['']) {
if(Object.keys(edits).length > 1) {
Loggers.warn('Full array edits are incompatible with other edits',
componentType);
}
var fullVal = edits[''][''];
if(isRemoveVal(fullVal)) np.set(null);
else if(Array.isArray(fullVal)) np.set(fullVal);
else {
Loggers.warn('Unrecognized full array edit value', componentType, fullVal);
return true;
}
if(replotLater) return false;
supplyComponentDefaults(layout, fullLayout);
draw(gd);
return true;
}
var componentNums = Object.keys(edits).map(Number).sort(),
componentArrayIn = np.get(),
componentArray = componentArrayIn || [],
// componentArrayFull is used just to keep splices in line between
// full and input arrays, so private keys can be copied over after
// redoing supplyDefaults
// TODO: this assumes componentArray is in gd.layout - which will not be
// true after we extend this to restyle
componentArrayFull = nestedProperty(fullLayout, componentType).get();
var deletes = [],
firstIndexChange = -1,
maxIndex = componentArray.length,
i,
j,
componentNum,
objEdits,
objKeys,
objVal,
adding;
// first make the add and edit changes
for(i = 0; i < componentNums.length; i++) {
componentNum = componentNums[i];
objEdits = edits[componentNum];
objKeys = Object.keys(objEdits);
objVal = objEdits[''],
adding = isAddVal(objVal);
if(componentNum < 0 || componentNum > componentArray.length - (adding ? 0 : 1)) {
Loggers.warn('index out of range', componentType, componentNum);
continue;
}
if(objVal !== undefined) {
if(objKeys.length > 1) {
Loggers.warn(
'Insertion & removal are incompatible with edits to the same index.',
componentType, componentNum);
}
if(isRemoveVal(objVal)) {
deletes.push(componentNum);
}
else if(adding) {
if(objVal === 'add') objVal = {};
componentArray.splice(componentNum, 0, objVal);
if(componentArrayFull) componentArrayFull.splice(componentNum, 0, {});
}
else {
Loggers.warn('Unrecognized full object edit value',
componentType, componentNum, objVal);
}
if(firstIndexChange === -1) firstIndexChange = componentNum;
}
else {
for(j = 0; j < objKeys.length; j++) {
nestedProperty(componentArray[componentNum], objKeys[j]).set(objEdits[objKeys[j]]);
}
}
}
// now do deletes
for(i = deletes.length - 1; i >= 0; i--) {
componentArray.splice(deletes[i], 1);
// TODO: this drops private keys that had been stored in componentArrayFull
// does this have any ill effects?
if(componentArrayFull) componentArrayFull.splice(deletes[i], 1);
}
if(!componentArray.length) np.set(null);
else if(!componentArrayIn) np.set(componentArray);
if(replotLater) return false;
supplyComponentDefaults(layout, fullLayout);
// finally draw all the components we need to
// if we added or removed any, redraw all after it
if(drawOne !== noop) {
var indicesToDraw;
if(firstIndexChange === -1) {
// there's no re-indexing to do, so only redraw components that changed
indicesToDraw = componentNums;
}
else {
// in case the component array was shortened, we still need do call
// drawOne on the latter items so they get properly removed
maxIndex = Math.max(componentArray.length, maxIndex);
indicesToDraw = [];
for(i = 0; i < componentNums.length; i++) {
componentNum = componentNums[i];
if(componentNum >= firstIndexChange) break;
indicesToDraw.push(componentNum);
}
for(i = firstIndexChange; i < maxIndex; i++) {
indicesToDraw.push(i);
}
}
for(i = 0; i < indicesToDraw.length; i++) {
drawOne(gd, indicesToDraw[i]);
}
}
else draw(gd);
return true;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Plotly = require('../plotly');
var Lib = require('../lib');
var Events = require('../lib/events');
var Queue = require('../lib/queue');
var Registry = require('../registry');
var Plots = require('../plots/plots');
var Fx = require('../plots/cartesian/graph_interact');
var Polar = require('../plots/polar');
var Drawing = require('../components/drawing');
var ErrorBars = require('../components/errorbars');
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
var svgTextUtils = require('../lib/svg_text_utils');
var manageArrays = require('./manage_arrays');
var helpers = require('./helpers');
var subroutines = require('./subroutines');
var cartesianConstants = require('../plots/cartesian/constants');
var enforceAxisConstraints = require('../plots/cartesian/constraints');
var axisIds = require('../plots/cartesian/axis_ids');
/**
* Main plot-creation function
*
* @param {string id or DOM element} gd
* the id or DOM element of the graph container div
* @param {array of objects} data
* array of traces, containing the data and display information for each trace
* @param {object} layout
* object describing the overall display of the plot,
* all the stuff that doesn't pertain to any individual trace
* @param {object} config
* configuration options (see ./plot_config.js for more info)
*
*/
Plotly.plot = function(gd, data, layout, config) {
var frames;
gd = helpers.getGraphDiv(gd);
// Events.init is idempotent and bails early if gd has already been init'd
Events.init(gd);
if(Lib.isPlainObject(data)) {
var obj = data;
data = obj.data;
layout = obj.layout;
config = obj.config;
frames = obj.frames;
}
var okToPlot = Events.triggerHandler(gd, 'plotly_beforeplot', [data, layout, config]);
if(okToPlot === false) return Promise.reject();
// if there's no data or layout, and this isn't yet a plotly plot
// container, log a warning to help plotly.js users debug
if(!data && !layout && !Lib.isPlotDiv(gd)) {
Lib.warn('Calling Plotly.plot as if redrawing ' +
'but this container doesn\'t yet have a plot.', gd);
}
function addFrames() {
if(frames) {
return Plotly.addFrames(gd, frames);
}
}
// transfer configuration options to gd until we move over to
// a more OO like model
setPlotContext(gd, config);
if(!layout) layout = {};
// hook class for plots main container (in case of plotly.js
// this won't be #embedded-graph or .js-tab-contents)
d3.select(gd).classed('js-plotly-plot', true);
// off-screen getBoundingClientRect testing space,
// in #js-plotly-tester (and stored as gd._tester)
// so we can share cached text across tabs
Drawing.makeTester(gd);
// collect promises for any async actions during plotting
// any part of the plotting code can push to gd._promises, then
// before we move to the next step, we check that they're all
// complete, and empty out the promise list again.
gd._promises = [];
var graphWasEmpty = ((gd.data || []).length === 0 && Array.isArray(data));
// if there is already data on the graph, append the new data
// if you only want to redraw, pass a non-array for data
if(Array.isArray(data)) {
helpers.cleanData(data, gd.data);
if(graphWasEmpty) gd.data = data;
else gd.data.push.apply(gd.data, data);
// for routines outside graph_obj that want a clean tab
// (rather than appending to an existing one) gd.empty
// is used to determine whether to make a new tab
gd.empty = false;
}
if(!gd.layout || graphWasEmpty) gd.layout = helpers.cleanLayout(layout);
// if the user is trying to drag the axes, allow new data and layout
// to come in but don't allow a replot.
if(gd._dragging && !gd._transitioning) {
// signal to drag handler that after everything else is done
// we need to replot, because something has changed
gd._replotPending = true;
return Promise.reject();
} else {
// we're going ahead with a replot now
gd._replotPending = false;
}
Plots.supplyDefaults(gd);
var fullLayout = gd._fullLayout;
// Polar plots
if(data && data[0] && data[0].r) return plotPolar(gd, data, layout);
// so we don't try to re-call Plotly.plot from inside
// legend and colorbar, if margins changed
fullLayout._replotting = true;
// make or remake the framework if we need to
if(graphWasEmpty) makePlotFramework(gd);
// polar need a different framework
if(gd.framework !== makePlotFramework) {
gd.framework = makePlotFramework;
makePlotFramework(gd);
}
// save initial show spikes once per graph
if(graphWasEmpty) Plotly.Axes.saveShowSpikeInitial(gd);
// prepare the data and find the autorange
// generate calcdata, if we need to
// to force redoing calcdata, just delete it before calling Plotly.plot
var recalc = !gd.calcdata || gd.calcdata.length !== (gd._fullData || []).length;
if(recalc) Plots.doCalcdata(gd);
// in case it has changed, attach fullData traces to calcdata
for(var i = 0; i < gd.calcdata.length; i++) {
gd.calcdata[i][0].trace = gd._fullData[i];
}
/*
* start async-friendly code - now we're actually drawing things
*/
var oldmargins = JSON.stringify(fullLayout._size);
// draw framework first so that margin-pushing
// components can position themselves correctly
function drawFramework() {
var basePlotModules = fullLayout._basePlotModules;
for(var i = 0; i < basePlotModules.length; i++) {
if(basePlotModules[i].drawFramework) {
basePlotModules[i].drawFramework(gd);
}
}
return Lib.syncOrAsync([
subroutines.layoutStyles,
drawAxes,
Fx.init
], gd);
}
// draw anything that can affect margins.
function marginPushers() {
var calcdata = gd.calcdata;
var i, cd, trace;
Registry.getComponentMethod('legend', 'draw')(gd);
Registry.getComponentMethod('rangeselector', 'draw')(gd);
Registry.getComponentMethod('sliders', 'draw')(gd);
Registry.getComponentMethod('updatemenus', 'draw')(gd);
for(i = 0; i < calcdata.length; i++) {
cd = calcdata[i];
trace = cd[0].trace;
if(trace.visible !== true || !trace._module.colorbar) {
Plots.autoMargin(gd, 'cb' + trace.uid);
}
else trace._module.colorbar(gd, cd);
}
Plots.doAutoMargin(gd);
return Plots.previousPromises(gd);
}
// in case the margins changed, draw margin pushers again
function marginPushersAgain() {
var seq = JSON.stringify(fullLayout._size) === oldmargins ?
[] :
[marginPushers, subroutines.layoutStyles];
// re-initialize cartesian interaction,
// which are sometimes cleared during marginPushers
seq = seq.concat(Fx.init);
return Lib.syncOrAsync(seq, gd);
}
function positionAndAutorange() {
if(!recalc) return;
var subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
modules = fullLayout._modules;
// position and range calculations for traces that
// depend on each other ie bars (stacked or grouped)
// and boxes (grouped) push each other out of the way
var subplotInfo, _module;
for(var i = 0; i < subplots.length; i++) {
subplotInfo = fullLayout._plots[subplots[i]];
for(var j = 0; j < modules.length; j++) {
_module = modules[j];
if(_module.setPositions) _module.setPositions(gd, subplotInfo);
}
}
// calc and autorange for errorbars
ErrorBars.calc(gd);
// TODO: autosize extra for text markers and images
// see https://github.com/plotly/plotly.js/issues/1111
return Lib.syncOrAsync([
Registry.getComponentMethod('shapes', 'calcAutorange'),
Registry.getComponentMethod('annotations', 'calcAutorange'),
doAutoRangeAndConstraints,
Registry.getComponentMethod('rangeslider', 'calcAutorange')
], gd);
}
function doAutoRangeAndConstraints() {
if(gd._transitioning) return;
var axList = Plotly.Axes.list(gd, '', true);
for(var i = 0; i < axList.length; i++) {
Plotly.Axes.doAutoRange(axList[i]);
}
enforceAxisConstraints(gd);
// store initial ranges *after* enforcing constraints, otherwise
// we will never look like we're at the initial ranges
if(graphWasEmpty) Plotly.Axes.saveRangeInitial(gd);
}
// draw ticks, titles, and calculate axis scaling (._b, ._m)
function drawAxes() {
return Plotly.Axes.doTicks(gd, 'redraw');
}
// Now plot the data
function drawData() {
var calcdata = gd.calcdata,
i;
// in case of traces that were heatmaps or contour maps
// previously, remove them and their colorbars explicitly
for(i = 0; i < calcdata.length; i++) {
var trace = calcdata[i][0].trace,
isVisible = (trace.visible === true),
uid = trace.uid;
if(!isVisible || !Registry.traceIs(trace, '2dMap')) {
var query = (
'.hm' + uid +
',.contour' + uid +
',#clip' + uid
);
fullLayout._paper
.selectAll(query)
.remove();
fullLayout._infolayer.selectAll('g.rangeslider-container')
.selectAll(query)
.remove();
}
if(!isVisible || !trace._module.colorbar) {
fullLayout._infolayer.selectAll('.cb' + uid).remove();
}
}
// loop over the base plot modules present on graph
var basePlotModules = fullLayout._basePlotModules;
for(i = 0; i < basePlotModules.length; i++) {
basePlotModules[i].plot(gd);
}
// keep reference to shape layers in subplots
var layerSubplot = fullLayout._paper.selectAll('.layer-subplot');
fullLayout._shapeSubplotLayers = layerSubplot.selectAll('.shapelayer');
// styling separate from drawing
Plots.style(gd);
// show annotations and shapes
Registry.getComponentMethod('shapes', 'draw')(gd);
Registry.getComponentMethod('annotations', 'draw')(gd);
// source links
Plots.addLinks(gd);
// Mark the first render as complete
fullLayout._replotting = false;
return Plots.previousPromises(gd);
}
// An initial paint must be completed before these components can be
// correctly sized and the whole plot re-margined. fullLayout._replotting must
// be set to false before these will work properly.
function finalDraw() {
Registry.getComponentMethod('shapes', 'draw')(gd);
Registry.getComponentMethod('images', 'draw')(gd);
Registry.getComponentMethod('annotations', 'draw')(gd);
Registry.getComponentMethod('legend', 'draw')(gd);
Registry.getComponentMethod('rangeslider', 'draw')(gd);
Registry.getComponentMethod('rangeselector', 'draw')(gd);
Registry.getComponentMethod('sliders', 'draw')(gd);
Registry.getComponentMethod('updatemenus', 'draw')(gd);
}
var seq = [
Plots.previousPromises,
addFrames,
drawFramework,
marginPushers,
marginPushersAgain,
positionAndAutorange,
subroutines.layoutStyles,
drawAxes,
drawData,
finalDraw,
Plots.rehover
];
Lib.syncOrAsync(seq, gd);
// even if everything we did was synchronous, return a promise
// so that the caller doesn't care which route we took
return Promise.all(gd._promises).then(function() {
gd.emit('plotly_afterplot');
return gd;
});
};
function opaqueSetBackground(gd, bgColor) {
gd._fullLayout._paperdiv.style('background', 'white');
Plotly.defaultConfig.setBackground(gd, bgColor);
}
function setPlotContext(gd, config) {
if(!gd._context) gd._context = Lib.extendFlat({}, Plotly.defaultConfig);
var context = gd._context;
if(config) {
Object.keys(config).forEach(function(key) {
if(key in context) {
if(key === 'setBackground' && config[key] === 'opaque') {
context[key] = opaqueSetBackground;
}
else context[key] = config[key];
}
});
// map plot3dPixelRatio to plotGlPixelRatio for backward compatibility
if(config.plot3dPixelRatio && !context.plotGlPixelRatio) {
context.plotGlPixelRatio = context.plot3dPixelRatio;
}
}
// staticPlot forces a bunch of others:
if(context.staticPlot) {
context.editable = false;
context.autosizable = false;
context.scrollZoom = false;
context.doubleClick = false;
context.showTips = false;
context.showLink = false;
context.displayModeBar = false;
}
}
function plotPolar(gd, data, layout) {
// build or reuse the container skeleton
var plotContainer = d3.select(gd).selectAll('.plot-container')
.data([0]);
plotContainer.enter()
.insert('div', ':first-child')
.classed('plot-container plotly', true);
var paperDiv = plotContainer.selectAll('.svg-container')
.data([0]);
paperDiv.enter().append('div')
.classed('svg-container', true)
.style('position', 'relative');
// empty it everytime for now
paperDiv.html('');
// fulfill gd requirements
if(data) gd.data = data;
if(layout) gd.layout = layout;
Polar.manager.fillLayout(gd);
// resize canvas
paperDiv.style({
width: gd._fullLayout.width + 'px',
height: gd._fullLayout.height + 'px'
});
// instantiate framework
gd.framework = Polar.manager.framework(gd);
// plot
gd.framework({data: gd.data, layout: gd.layout}, paperDiv.node());
// set undo point
gd.framework.setUndoPoint();
// get the resulting svg for extending it
var polarPlotSVG = gd.framework.svg();
// editable title
var opacity = 1;
var txt = gd._fullLayout.title;
if(txt === '' || !txt) opacity = 0;
var placeholderText = 'Click to enter title';
var titleLayout = function() {
this.call(svgTextUtils.convertToTspans);
// TODO: html/mathjax
// TODO: center title
};
var title = polarPlotSVG.select('.title-group text')
.call(titleLayout);
if(gd._context.editable) {
title.attr({'data-unformatted': txt});
if(!txt || txt === placeholderText) {
opacity = 0.2;
title.attr({'data-unformatted': placeholderText})
.text(placeholderText)
.style({opacity: opacity})
.on('mouseover.opacity', function() {
d3.select(this).transition().duration(100)
.style('opacity', 1);
})
.on('mouseout.opacity', function() {
d3.select(this).transition().duration(1000)
.style('opacity', 0);
});
}
var setContenteditable = function() {
this.call(svgTextUtils.makeEditable)
.on('edit', function(text) {
gd.framework({layout: {title: text}});
this.attr({'data-unformatted': text})
.text(text)
.call(titleLayout);
this.call(setContenteditable);
})
.on('cancel', function() {
var txt = this.attr('data-unformatted');
this.text(txt).call(titleLayout);
});
};
title.call(setContenteditable);
}
gd._context.setBackground(gd, gd._fullLayout.paper_bgcolor);
Plots.addLinks(gd);
return Promise.resolve();
}
// convenience function to force a full redraw, mostly for use by plotly.js
Plotly.redraw = function(gd) {
gd = helpers.getGraphDiv(gd);
if(!Lib.isPlotDiv(gd)) {
throw new Error('This element is not a Plotly plot: ' + gd);
}
helpers.cleanData(gd.data, gd.data);
helpers.cleanLayout(gd.layout);
gd.calcdata = undefined;
return Plotly.plot(gd).then(function() {
gd.emit('plotly_redraw');
return gd;
});
};
/**
* Convenience function to make idempotent plot option obvious to users.
*
* @param gd
* @param {Object[]} data
* @param {Object} layout
* @param {Object} config
*/
Plotly.newPlot = function(gd, data, layout, config) {
gd = helpers.getGraphDiv(gd);
// remove gl contexts
Plots.cleanPlot([], {}, gd._fullData || {}, gd._fullLayout || {});
Plots.purge(gd);
return Plotly.plot(gd, data, layout, config);
};
/**
* Wrap negative indicies to their positive counterparts.
*
* @param {Number[]} indices An array of indices
* @param {Number} maxIndex The maximum index allowable (arr.length - 1)
*/
function positivifyIndices(indices, maxIndex) {
var parentLength = maxIndex + 1,
positiveIndices = [],
i,
index;
for(i = 0; i < indices.length; i++) {
index = indices[i];
if(index < 0) {
positiveIndices.push(parentLength + index);
} else {
positiveIndices.push(index);
}
}
return positiveIndices;
}
/**
* Ensures that an index array for manipulating gd.data is valid.
*
* Intended for use with addTraces, deleteTraces, and moveTraces.
*
* @param gd
* @param indices
* @param arrayName
*/
function assertIndexArray(gd, indices, arrayName) {
var i,
index;
for(i = 0; i < indices.length; i++) {
index = indices[i];
// validate that indices are indeed integers
if(index !== parseInt(index, 10)) {
throw new Error('all values in ' + arrayName + ' must be integers');
}
// check that all indices are in bounds for given gd.data array length
if(index >= gd.data.length || index < -gd.data.length) {
throw new Error(arrayName + ' must be valid indices for gd.data.');
}
// check that indices aren't repeated
if(indices.indexOf(index, i + 1) > -1 ||
index >= 0 && indices.indexOf(-gd.data.length + index) > -1 ||
index < 0 && indices.indexOf(gd.data.length + index) > -1) {
throw new Error('each index in ' + arrayName + ' must be unique.');
}
}
}
/**
* Private function used by Plotly.moveTraces to check input args
*
* @param gd
* @param currentIndices
* @param newIndices
*/
function checkMoveTracesArgs(gd, currentIndices, newIndices) {
// check that gd has attribute 'data' and 'data' is array
if(!Array.isArray(gd.data)) {
throw new Error('gd.data must be an array.');
}
// validate currentIndices array
if(typeof currentIndices === 'undefined') {
throw new Error('currentIndices is a required argument.');
} else if(!Array.isArray(currentIndices)) {
currentIndices = [currentIndices];
}
assertIndexArray(gd, currentIndices, 'currentIndices');
// validate newIndices array if it exists
if(typeof newIndices !== 'undefined' && !Array.isArray(newIndices)) {
newIndices = [newIndices];
}
if(typeof newIndices !== 'undefined') {
assertIndexArray(gd, newIndices, 'newIndices');
}
// check currentIndices and newIndices are the same length if newIdices exists
if(typeof newIndices !== 'undefined' && currentIndices.length !== newIndices.length) {
throw new Error('current and new indices must be of equal length.');
}
}
/**
* A private function to reduce the type checking clutter in addTraces.
*
* @param gd
* @param traces
* @param newIndices
*/
function checkAddTracesArgs(gd, traces, newIndices) {
var i, value;
// check that gd has attribute 'data' and 'data' is array
if(!Array.isArray(gd.data)) {
throw new Error('gd.data must be an array.');
}
// make sure traces exists
if(typeof traces === 'undefined') {
throw new Error('traces must be defined.');
}
// make sure traces is an array
if(!Array.isArray(traces)) {
traces = [traces];
}
// make sure each value in traces is an object
for(i = 0; i < traces.length; i++) {
value = traces[i];
if(typeof value !== 'object' || (Array.isArray(value) || value === null)) {
throw new Error('all values in traces array must be non-array objects');
}
}
// make sure we have an index for each trace
if(typeof newIndices !== 'undefined' && !Array.isArray(newIndices)) {
newIndices = [newIndices];
}
if(typeof newIndices !== 'undefined' && newIndices.length !== traces.length) {
throw new Error(
'if indices is specified, traces.length must equal indices.length'
);
}
}
/**
* A private function to reduce the type checking clutter in spliceTraces.
* Get all update Properties from gd.data. Validate inputs and outputs.
* Used by prependTrace and extendTraces
*
* @param gd
* @param update
* @param indices
* @param maxPoints
*/
function assertExtendTracesArgs(gd, update, indices, maxPoints) {
var maxPointsIsObject = Lib.isPlainObject(maxPoints);
if(!Array.isArray(gd.data)) {
throw new Error('gd.data must be an array');
}
if(!Lib.isPlainObject(update)) {
throw new Error('update must be a key:value object');
}
if(typeof indices === 'undefined') {
throw new Error('indices must be an integer or array of integers');
}
assertIndexArray(gd, indices, 'indices');
for(var key in update) {
/*
* Verify that the attribute to be updated contains as many trace updates
* as indices. Failure must result in throw and no-op
*/
if(!Array.isArray(update[key]) || update[key].length !== indices.length) {
throw new Error('attribute ' + key + ' must be an array of length equal to indices array length');
}
/*
* if maxPoints is an object it must match keys and array lengths of 'update' 1:1
*/
if(maxPointsIsObject &&
(!(key in maxPoints) || !Array.isArray(maxPoints[key]) ||
maxPoints[key].length !== update[key].length)) {
throw new Error('when maxPoints is set as a key:value object it must contain a 1:1 ' +
'corrispondence with the keys and number of traces in the update object');
}
}
}
/**
* A private function to reduce the type checking clutter in spliceTraces.
*
* @param {Object|HTMLDivElement} gd
* @param {Object} update
* @param {Number[]} indices
* @param {Number||Object} maxPoints
* @return {Object[]}
*/
function getExtendProperties(gd, update, indices, maxPoints) {
var maxPointsIsObject = Lib.isPlainObject(maxPoints),
updateProps = [];
var trace, target, prop, insert, maxp;
// allow scalar index to represent a single trace position
if(!Array.isArray(indices)) indices = [indices];
// negative indices are wrapped around to their positive value. Equivalent to python indexing.
indices = positivifyIndices(indices, gd.data.length - 1);
// loop through all update keys and traces and harvest validated data.
for(var key in update) {
for(var j = 0; j < indices.length; j++) {
/*
* Choose the trace indexed by the indices map argument and get the prop setter-getter
* instance that references the key and value for this particular trace.
*/
trace = gd.data[indices[j]];
prop = Lib.nestedProperty(trace, key);
/*
* Target is the existing gd.data.trace.dataArray value like "x" or "marker.size"
* Target must exist as an Array to allow the extend operation to be performed.
*/
target = prop.get();
insert = update[key][j];
if(!Array.isArray(insert)) {
throw new Error('attribute: ' + key + ' index: ' + j + ' must be an array');
}
if(!Array.isArray(target)) {
throw new Error('cannot extend missing or non-array attribute: ' + key);
}
/*
* maxPoints may be an object map or a scalar. If object select the key:value, else
* Use the scalar maxPoints for all key and trace combinations.
*/
maxp = maxPointsIsObject ? maxPoints[key][j] : maxPoints;
// could have chosen null here, -1 just tells us to not take a window
if(!isNumeric(maxp)) maxp = -1;
/*
* Wrap the nestedProperty in an object containing required data
* for lengthening and windowing this particular trace - key combination.
* Flooring maxp mirrors the behaviour of floats in the Array.slice JSnative function.
*/
updateProps.push({
prop: prop,
target: target,
insert: insert,
maxp: Math.floor(maxp)
});
}
}
// all target and insertion data now validated
return updateProps;
}
/**
* A private function to key Extend and Prepend traces DRY
*
* @param {Object|HTMLDivElement} gd
* @param {Object} update
* @param {Number[]} indices
* @param {Number||Object} maxPoints
* @param {Function} lengthenArray
* @param {Function} spliceArray
* @return {Object}
*/
function spliceTraces(gd, update, indices, maxPoints, lengthenArray, spliceArray) {
assertExtendTracesArgs(gd, update, indices, maxPoints);
var updateProps = getExtendProperties(gd, update, indices, maxPoints),
remainder = [],
undoUpdate = {},
undoPoints = {};
var target, prop, maxp;
for(var i = 0; i < updateProps.length; i++) {
/*
* prop is the object returned by Lib.nestedProperties
*/
prop = updateProps[i].prop;
maxp = updateProps[i].maxp;
target = lengthenArray(updateProps[i].target, updateProps[i].insert);
/*
* If maxp is set within post-extension trace.length, splice to maxp length.
* Otherwise skip function call as splice op will have no effect anyway.
*/
if(maxp >= 0 && maxp < target.length) remainder = spliceArray(target, maxp);
/*
* to reverse this operation we need the size of the original trace as the reverse
* operation will need to window out any lengthening operation performed in this pass.
*/
maxp = updateProps[i].target.length;
/*
* Magic happens here! update gd.data.trace[key] with new array data.
*/
prop.set(target);
if(!Array.isArray(undoUpdate[prop.astr])) undoUpdate[prop.astr] = [];
if(!Array.isArray(undoPoints[prop.astr])) undoPoints[prop.astr] = [];
/*
* build the inverse update object for the undo operation
*/
undoUpdate[prop.astr].push(remainder);
/*
* build the matching maxPoints undo object containing original trace lengths.
*/
undoPoints[prop.astr].push(maxp);
}
return {update: undoUpdate, maxPoints: undoPoints};
}
/**
* extend && prepend traces at indices with update arrays, window trace lengths to maxPoints
*
* Extend and Prepend have identical APIs. Prepend inserts an array at the head while Extend
* inserts an array off the tail. Prepend truncates the tail of the array - counting maxPoints
* from the head, whereas Extend truncates the head of the array, counting backward maxPoints
* from the tail.
*
* If maxPoints is undefined, nonNumeric, negative or greater than extended trace length no
* truncation / windowing will be performed. If its zero, well the whole trace is truncated.
*
* @param {Object|HTMLDivElement} gd The graph div
* @param {Object} update The key:array map of target attributes to extend
* @param {Number|Number[]} indices The locations of traces to be extended
* @param {Number|Object} [maxPoints] Number of points for trace window after lengthening.
*
*/
Plotly.extendTraces = function extendTraces(gd, update, indices, maxPoints) {
gd = helpers.getGraphDiv(gd);
var undo = spliceTraces(gd, update, indices, maxPoints,
/*
* The Lengthen operation extends trace from end with insert
*/
function(target, insert) {
return target.concat(insert);
},
/*
* Window the trace keeping maxPoints, counting back from the end
*/
function(target, maxPoints) {
return target.splice(0, target.length - maxPoints);
});
var promise = Plotly.redraw(gd);
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
Queue.add(gd, Plotly.prependTraces, undoArgs, extendTraces, arguments);
return promise;
};
Plotly.prependTraces = function prependTraces(gd, update, indices, maxPoints) {
gd = helpers.getGraphDiv(gd);
var undo = spliceTraces(gd, update, indices, maxPoints,
/*
* The Lengthen operation extends trace by appending insert to start
*/
function(target, insert) {
return insert.concat(target);
},
/*
* Window the trace keeping maxPoints, counting forward from the start
*/
function(target, maxPoints) {
return target.splice(maxPoints, target.length);
});
var promise = Plotly.redraw(gd);
var undoArgs = [gd, undo.update, indices, undo.maxPoints];
Queue.add(gd, Plotly.extendTraces, undoArgs, prependTraces, arguments);
return promise;
};
/**
* Add data traces to an existing graph div.
*
* @param {Object|HTMLDivElement} gd The graph div
* @param {Object[]} gd.data The array of traces we're adding to
* @param {Object[]|Object} traces The object or array of objects to add
* @param {Number[]|Number} [newIndices=[gd.data.length]] Locations to add traces
*
*/
Plotly.addTraces = function addTraces(gd, traces, newIndices) {
gd = helpers.getGraphDiv(gd);
var currentIndices = [],
undoFunc = Plotly.deleteTraces,
redoFunc = addTraces,
undoArgs = [gd, currentIndices],
redoArgs = [gd, traces], // no newIndices here
i,
promise;
// all validation is done elsewhere to remove clutter here
checkAddTracesArgs(gd, traces, newIndices);
// make sure traces is an array
if(!Array.isArray(traces)) {
traces = [traces];
}
// make sure traces do not repeat existing ones
traces = traces.map(function(trace) {
return Lib.extendFlat({}, trace);
});
helpers.cleanData(traces, gd.data);
// add the traces to gd.data (no redrawing yet!)
for(i = 0; i < traces.length; i++) {
gd.data.push(traces[i]);
}
// to continue, we need to call moveTraces which requires currentIndices
for(i = 0; i < traces.length; i++) {
currentIndices.push(-traces.length + i);
}
// if the user didn't define newIndices, they just want the traces appended
// i.e., we can simply redraw and be done
if(typeof newIndices === 'undefined') {
promise = Plotly.redraw(gd);
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return promise;
}
// make sure indices is property defined
if(!Array.isArray(newIndices)) {
newIndices = [newIndices];
}
try {
// this is redundant, but necessary to not catch later possible errors!
checkMoveTracesArgs(gd, currentIndices, newIndices);
}
catch(error) {
// something went wrong, reset gd to be safe and rethrow error
gd.data.splice(gd.data.length - traces.length, traces.length);
throw error;
}
// if we're here, the user has defined specific places to place the new traces
// this requires some extra work that moveTraces will do
Queue.startSequence(gd);
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
promise = Plotly.moveTraces(gd, currentIndices, newIndices);
Queue.stopSequence(gd);
return promise;
};
/**
* Delete traces at `indices` from gd.data array.
*
* @param {Object|HTMLDivElement} gd The graph div
* @param {Object[]} gd.data The array of traces we're removing from
* @param {Number|Number[]} indices The indices
*/
Plotly.deleteTraces = function deleteTraces(gd, indices) {
gd = helpers.getGraphDiv(gd);
var traces = [],
undoFunc = Plotly.addTraces,
redoFunc = deleteTraces,
undoArgs = [gd, traces, indices],
redoArgs = [gd, indices],
i,
deletedTrace;
// make sure indices are defined
if(typeof indices === 'undefined') {
throw new Error('indices must be an integer or array of integers.');
} else if(!Array.isArray(indices)) {
indices = [indices];
}
assertIndexArray(gd, indices, 'indices');
// convert negative indices to positive indices
indices = positivifyIndices(indices, gd.data.length - 1);
// we want descending here so that splicing later doesn't affect indexing
indices.sort(Lib.sorterDes);
for(i = 0; i < indices.length; i += 1) {
deletedTrace = gd.data.splice(indices[i], 1)[0];
traces.push(deletedTrace);
}
var promise = Plotly.redraw(gd);
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return promise;
};
/**
* Move traces at currentIndices array to locations in newIndices array.
*
* If newIndices is omitted, currentIndices will be moved to the end. E.g.,
* these are equivalent:
*
* Plotly.moveTraces(gd, [1, 2, 3], [-3, -2, -1])
* Plotly.moveTraces(gd, [1, 2, 3])
*
* @param {Object|HTMLDivElement} gd The graph div
* @param {Object[]} gd.data The array of traces we're removing from
* @param {Number|Number[]} currentIndices The locations of traces to be moved
* @param {Number|Number[]} [newIndices] The locations to move traces to
*
* Example calls:
*
* // move trace i to location x
* Plotly.moveTraces(gd, i, x)
*
* // move trace i to end of array
* Plotly.moveTraces(gd, i)
*
* // move traces i, j, k to end of array (i != j != k)
* Plotly.moveTraces(gd, [i, j, k])
*
* // move traces [i, j, k] to [x, y, z] (i != j != k) (x != y != z)
* Plotly.moveTraces(gd, [i, j, k], [x, y, z])
*
* // reorder all traces (assume there are 5--a, b, c, d, e)
* Plotly.moveTraces(gd, [b, d, e, a, c]) // same as 'move to end'
*/
Plotly.moveTraces = function moveTraces(gd, currentIndices, newIndices) {
gd = helpers.getGraphDiv(gd);
var newData = [],
movingTraceMap = [],
undoFunc = moveTraces,
redoFunc = moveTraces,
undoArgs = [gd, newIndices, currentIndices],
redoArgs = [gd, currentIndices, newIndices],
i;
// to reduce complexity here, check args elsewhere
// this throws errors where appropriate
checkMoveTracesArgs(gd, currentIndices, newIndices);
// make sure currentIndices is an array
currentIndices = Array.isArray(currentIndices) ? currentIndices : [currentIndices];
// if undefined, define newIndices to point to the end of gd.data array
if(typeof newIndices === 'undefined') {
newIndices = [];
for(i = 0; i < currentIndices.length; i++) {
newIndices.push(-currentIndices.length + i);
}
}
// make sure newIndices is an array if it's user-defined
newIndices = Array.isArray(newIndices) ? newIndices : [newIndices];
// convert negative indices to positive indices (they're the same length)
currentIndices = positivifyIndices(currentIndices, gd.data.length - 1);
newIndices = positivifyIndices(newIndices, gd.data.length - 1);
// at this point, we've coerced the index arrays into predictable forms
// get the traces that aren't being moved around
for(i = 0; i < gd.data.length; i++) {
// if index isn't in currentIndices, include it in ignored!
if(currentIndices.indexOf(i) === -1) {
newData.push(gd.data[i]);
}
}
// get a mapping of indices to moving traces
for(i = 0; i < currentIndices.length; i++) {
movingTraceMap.push({newIndex: newIndices[i], trace: gd.data[currentIndices[i]]});
}
// reorder this mapping by newIndex, ascending
movingTraceMap.sort(function(a, b) {
return a.newIndex - b.newIndex;
});
// now, add the moving traces back in, in order!
for(i = 0; i < movingTraceMap.length; i += 1) {
newData.splice(movingTraceMap[i].newIndex, 0, movingTraceMap[i].trace);
}
gd.data = newData;
var promise = Plotly.redraw(gd);
Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return promise;
};
/**
* restyle: update trace attributes of an existing plot
*
* Can be called two ways.
*
* Signature 1:
* @param {String | HTMLDivElement} gd
* the id or DOM element of the graph container div
* @param {String} astr
* attribute string (like `'marker.symbol'`) to update
* @param {*} val
* value to give this attribute
* @param {Number[] | Number} [traces]
* integer or array of integers for the traces to alter (all if omitted)
*
* Signature 2:
* @param {String | HTMLDivElement} gd
* (as in signature 1)
* @param {Object} aobj
* attribute object `{astr1: val1, astr2: val2 ...}`
* allows setting multiple attributes simultaneously
* @param {Number[] | Number} [traces]
* (as in signature 1)
*
* `val` (or `val1`, `val2` ... in the object form) can be an array,
* to apply different values to each trace.
*
* If the array is too short, it will wrap around (useful for
* style files that want to specify cyclical default values).
*/
Plotly.restyle = function restyle(gd, astr, val, traces) {
gd = helpers.getGraphDiv(gd);
helpers.clearPromiseQueue(gd);
var aobj = {};
if(typeof astr === 'string') aobj[astr] = val;
else if(Lib.isPlainObject(astr)) {
// the 3-arg form
aobj = Lib.extendFlat({}, astr);
if(traces === undefined) traces = val;
}
else {
Lib.warn('Restyle fail.', astr, val, traces);
return Promise.reject();
}
if(Object.keys(aobj).length) gd.changed = true;
var specs = _restyle(gd, aobj, traces),
flags = specs.flags;
// clear calcdata if required
if(flags.clearCalc) gd.calcdata = undefined;
// fill in redraw sequence
var seq = [];
if(flags.fullReplot) {
seq.push(Plotly.plot);
} else {
seq.push(Plots.previousPromises);
Plots.supplyDefaults(gd);
if(flags.dostyle) seq.push(subroutines.doTraceStyle);
if(flags.docolorbars) seq.push(subroutines.doColorBars);
}
seq.push(Plots.rehover);
Queue.add(gd,
restyle, [gd, specs.undoit, specs.traces],
restyle, [gd, specs.redoit, specs.traces]
);
var plotDone = Lib.syncOrAsync(seq, gd);
if(!plotDone || !plotDone.then) plotDone = Promise.resolve();
return plotDone.then(function() {
gd.emit('plotly_restyle', specs.eventData);
return gd;
});
};
function _restyle(gd, aobj, _traces) {
var fullLayout = gd._fullLayout,
fullData = gd._fullData,
data = gd.data,
i;
var traces = helpers.coerceTraceIndices(gd, _traces);
// initialize flags
var flags = {
docalc: false,
docalcAutorange: false,
doplot: false,
dostyle: false,
docolorbars: false,
autorangeOn: false,
clearCalc: false,
fullReplot: false
};
// copies of the change (and previous values of anything affected)
// for the undo / redo queue
var redoit = {},
undoit = {},
axlist,
flagAxForDelete = {};
// recalcAttrs attributes need a full regeneration of calcdata
// as well as a replot, because the right objects may not exist,
// or autorange may need recalculating
// in principle we generally shouldn't need to redo ALL traces... that's
// harder though.
var recalcAttrs = [
'mode', 'visible', 'type', 'orientation', 'fill',
'histfunc', 'histnorm', 'text',
'x', 'y', 'z',
'a', 'b', 'c',
'open', 'high', 'low', 'close',
'base', 'width', 'offset',
'xtype', 'x0', 'dx', 'ytype', 'y0', 'dy', 'xaxis', 'yaxis',
'line.width',
'connectgaps', 'transpose', 'zsmooth',
'showscale', 'marker.showscale',
'zauto', 'marker.cauto',
'autocolorscale', 'marker.autocolorscale',
'colorscale', 'marker.colorscale',
'reversescale', 'marker.reversescale',
'autobinx', 'nbinsx', 'xbins', 'xbins.start', 'xbins.end', 'xbins.size',
'autobiny', 'nbinsy', 'ybins', 'ybins.start', 'ybins.end', 'ybins.size',
'autocontour', 'ncontours', 'contours', 'contours.coloring',
'contours.operation', 'contours.value', 'contours.type', 'contours.value[0]', 'contours.value[1]',
'error_y', 'error_y.visible', 'error_y.value', 'error_y.type',
'error_y.traceref', 'error_y.array', 'error_y.symmetric',
'error_y.arrayminus', 'error_y.valueminus', 'error_y.tracerefminus',
'error_x', 'error_x.visible', 'error_x.value', 'error_x.type',
'error_x.traceref', 'error_x.array', 'error_x.symmetric',
'error_x.arrayminus', 'error_x.valueminus', 'error_x.tracerefminus',
'swapxy', 'swapxyaxes', 'orientationaxes',
'marker.colors', 'values', 'labels', 'label0', 'dlabel', 'sort',
'textinfo', 'textposition', 'textfont.size', 'textfont.family', 'textfont.color',
'insidetextfont.size', 'insidetextfont.family', 'insidetextfont.color',
'outsidetextfont.size', 'outsidetextfont.family', 'outsidetextfont.color',
'hole', 'scalegroup', 'domain', 'domain.x', 'domain.y',
'domain.x[0]', 'domain.x[1]', 'domain.y[0]', 'domain.y[1]',
'tilt', 'tiltaxis', 'depth', 'direction', 'rotation', 'pull',
'line.showscale', 'line.cauto', 'line.autocolorscale', 'line.reversescale',
'marker.line.showscale', 'marker.line.cauto', 'marker.line.autocolorscale', 'marker.line.reversescale',
'xcalendar', 'ycalendar',
'cumulative', 'cumulative.enabled', 'cumulative.direction', 'cumulative.currentbin',
'a0', 'da', 'b0', 'db', 'atype', 'btype',
'cheaterslope', 'carpet', 'sum',
];
var carpetAxisAttributes = [
'color', 'smoothing', 'title', 'titlefont', 'titlefont.size', 'titlefont.family',
'titlefont.color', 'titleoffset', 'type', 'autorange', 'rangemode', 'range',
'fixedrange', 'cheatertype', 'tickmode', 'nticks', 'tickvals', 'ticktext',
'ticks', 'mirror', 'ticklen', 'tickwidth', 'tickcolor', 'showticklabels',
'tickfont', 'tickfont.size', 'tickfont.family', 'tickfont.color', 'tickprefix',
'showtickprefix', 'ticksuffix', 'showticksuffix', 'showexponent', 'exponentformat',
'separatethousands', 'tickformat', 'categoryorder', 'categoryarray', 'labelpadding',
'labelprefix', 'labelsuffix', 'labelfont', 'labelfont.family', 'labelfont.size',
'labelfont.color', 'showline', 'linecolor', 'linewidth', 'gridcolor', 'gridwidth',
'showgrid', 'minorgridcount', 'minorgridwidth', 'minorgridcolor', 'startline',
'startlinecolor', 'startlinewidth', 'endline', 'endlinewidth', 'endlinecolor',
'tick0', 'dtick', 'arraytick0', 'arraydtick', 'hoverformat', 'tickangle'
];
for(i = 0; i < carpetAxisAttributes.length; i++) {
recalcAttrs.push('aaxis.' + carpetAxisAttributes[i]);
recalcAttrs.push('baxis.' + carpetAxisAttributes[i]);
}
for(i = 0; i < traces.length; i++) {
if(Registry.traceIs(fullData[traces[i]], 'box')) {
recalcAttrs.push('name');
break;
}
}
// autorangeAttrs attributes need a full redo of calcdata
// only if an axis is autoranged,
// because .calc() is where the autorange gets determined
// TODO: could we break this out as well?
var autorangeAttrs = [
'marker', 'marker.size', 'textfont',
'boxpoints', 'jitter', 'pointpos', 'whiskerwidth', 'boxmean',
'tickwidth'
];
// replotAttrs attributes need a replot (because different
// objects need to be made) but not a recalc
var replotAttrs = [
'zmin', 'zmax', 'zauto',
'xgap', 'ygap',
'marker.cmin', 'marker.cmax', 'marker.cauto',
'line.cmin', 'line.cmax',
'marker.line.cmin', 'marker.line.cmax',
'contours.start', 'contours.end', 'contours.size',
'contours.showlines',
'line', 'line.smoothing', 'line.shape',
'error_y.width', 'error_x.width', 'error_x.copy_ystyle',
'marker.maxdisplayed'
];
// these ones may alter the axis type
// (at least if the first trace is involved)
var axtypeAttrs = [
'type', 'x', 'y', 'x0', 'y0', 'orientation', 'xaxis', 'yaxis'
];
var zscl = ['zmin', 'zmax'],
xbins = ['xbins.start', 'xbins.end', 'xbins.size'],
ybins = ['ybins.start', 'ybins.end', 'ybins.size'],
contourAttrs = ['contours.start', 'contours.end', 'contours.size'];
// At the moment, only cartesian, pie and ternary plot types can afford
// to not go through a full replot
var doPlotWhiteList = ['cartesian', 'pie', 'ternary'];
fullLayout._basePlotModules.forEach(function(_module) {
if(doPlotWhiteList.indexOf(_module.name) === -1) flags.docalc = true;
});
// make a new empty vals array for undoit
function a0() { return traces.map(function() { return undefined; }); }
// for autoranging multiple axes
function addToAxlist(axid) {
var axName = Plotly.Axes.id2name(axid);
if(axlist.indexOf(axName) === -1) axlist.push(axName);
}
function autorangeAttr(axName) { return 'LAYOUT' + axName + '.autorange'; }
function rangeAttr(axName) { return 'LAYOUT' + axName + '.range'; }
// for attrs that interact (like scales & autoscales), save the
// old vals before making the change
// val=undefined will not set a value, just record what the value was.
// val=null will delete the attribute
// attr can be an array to set several at once (all to the same val)
function doextra(attr, val, i) {
if(Array.isArray(attr)) {
attr.forEach(function(a) { doextra(a, val, i); });
return;
}
// quit if explicitly setting this elsewhere
if(attr in aobj || helpers.hasParent(aobj, attr)) return;
var extraparam;
if(attr.substr(0, 6) === 'LAYOUT') {
extraparam = Lib.nestedProperty(gd.layout, attr.replace('LAYOUT', ''));
} else {
extraparam = Lib.nestedProperty(data[traces[i]], attr);
}
if(!(attr in undoit)) {
undoit[attr] = a0();
}
if(undoit[attr][i] === undefined) {
undoit[attr][i] = extraparam.get();
}
if(val !== undefined) {
extraparam.set(val);
}
}
// now make the changes to gd.data (and occasionally gd.layout)
// and figure out what kind of graphics update we need to do
for(var ai in aobj) {
if(helpers.hasParent(aobj, ai)) {
throw new Error('cannot set ' + ai + 'and a parent attribute simultaneously');
}
var vi = aobj[ai],
cont,
contFull,
param,
oldVal,
newVal;
redoit[ai] = vi;
if(ai.substr(0, 6) === 'LAYOUT') {
param = Lib.nestedProperty(gd.layout, ai.replace('LAYOUT', ''));
undoit[ai] = [param.get()];
// since we're allowing val to be an array, allow it here too,
// even though that's meaningless
param.set(Array.isArray(vi) ? vi[0] : vi);
// ironically, the layout attrs in restyle only require replot,
// not relayout
flags.docalc = true;
continue;
}
// set attribute in gd.data
undoit[ai] = a0();
for(i = 0; i < traces.length; i++) {
cont = data[traces[i]];
contFull = fullData[traces[i]];
param = Lib.nestedProperty(cont, ai);
oldVal = param.get();
newVal = Array.isArray(vi) ? vi[i % vi.length] : vi;
if(newVal === undefined) continue;
// setting bin or z settings should turn off auto
// and setting auto should save bin or z settings
if(zscl.indexOf(ai) !== -1) {
doextra('zauto', false, i);
}
else if(ai === 'colorscale') {
doextra('autocolorscale', false, i);
}
else if(ai === 'autocolorscale') {
doextra('colorscale', undefined, i);
}
else if(ai === 'marker.colorscale') {
doextra('marker.autocolorscale', false, i);
}
else if(ai === 'marker.autocolorscale') {
doextra('marker.colorscale', undefined, i);
}
else if(ai === 'zauto') {
doextra(zscl, undefined, i);
}
else if(xbins.indexOf(ai) !== -1) {
doextra('autobinx', false, i);
}
else if(ai === 'autobinx') {
doextra(xbins, undefined, i);
}
else if(ybins.indexOf(ai) !== -1) {
doextra('autobiny', false, i);
}
else if(ai === 'autobiny') {
doextra(ybins, undefined, i);
}
else if(contourAttrs.indexOf(ai) !== -1) {
doextra('autocontour', false, i);
}
else if(ai === 'autocontour') {
doextra(contourAttrs, undefined, i);
}
// heatmaps: setting x0 or dx, y0 or dy,
// should turn xtype/ytype to 'scaled' if 'array'
else if(['x0', 'dx'].indexOf(ai) !== -1 &&
contFull.x && contFull.xtype !== 'scaled') {
doextra('xtype', 'scaled', i);
}
else if(['y0', 'dy'].indexOf(ai) !== -1 &&
contFull.y && contFull.ytype !== 'scaled') {
doextra('ytype', 'scaled', i);
}
// changing colorbar size modes,
// make the resulting size not change
// note that colorbar fractional sizing is based on the
// original plot size, before anything (like a colorbar)
// increases the margins
else if(ai === 'colorbar.thicknessmode' && param.get() !== newVal &&
['fraction', 'pixels'].indexOf(newVal) !== -1 &&
contFull.colorbar) {
var thicknorm =
['top', 'bottom'].indexOf(contFull.colorbar.orient) !== -1 ?
(fullLayout.height - fullLayout.margin.t - fullLayout.margin.b) :
(fullLayout.width - fullLayout.margin.l - fullLayout.margin.r);
doextra('colorbar.thickness', contFull.colorbar.thickness *
(newVal === 'fraction' ? 1 / thicknorm : thicknorm), i);
}
else if(ai === 'colorbar.lenmode' && param.get() !== newVal &&
['fraction', 'pixels'].indexOf(newVal) !== -1 &&
contFull.colorbar) {
var lennorm =
['top', 'bottom'].indexOf(contFull.colorbar.orient) !== -1 ?
(fullLayout.width - fullLayout.margin.l - fullLayout.margin.r) :
(fullLayout.height - fullLayout.margin.t - fullLayout.margin.b);
doextra('colorbar.len', contFull.colorbar.len *
(newVal === 'fraction' ? 1 / lennorm : lennorm), i);
}
else if(ai === 'colorbar.tick0' || ai === 'colorbar.dtick') {
doextra('colorbar.tickmode', 'linear', i);
}
else if(ai === 'colorbar.tickmode') {
doextra(['colorbar.tick0', 'colorbar.dtick'], undefined, i);
}
if(ai === 'type' && (newVal === 'pie') !== (oldVal === 'pie')) {
var labelsTo = 'x',
valuesTo = 'y';
if((newVal === 'bar' || oldVal === 'bar') && cont.orientation === 'h') {
labelsTo = 'y';
valuesTo = 'x';
}
Lib.swapAttrs(cont, ['?', '?src'], 'labels', labelsTo);
Lib.swapAttrs(cont, ['d?', '?0'], 'label', labelsTo);
Lib.swapAttrs(cont, ['?', '?src'], 'values', valuesTo);
if(oldVal === 'pie') {
Lib.nestedProperty(cont, 'marker.color')
.set(Lib.nestedProperty(cont, 'marker.colors').get());
// super kludgy - but if all pies are gone we won't remove them otherwise
fullLayout._pielayer.selectAll('g.trace').remove();
} else if(Registry.traceIs(cont, 'cartesian')) {
Lib.nestedProperty(cont, 'marker.colors')
.set(Lib.nestedProperty(cont, 'marker.color').get());
// look for axes that are no longer in use and delete them
flagAxForDelete[cont.xaxis || 'x'] = true;
flagAxForDelete[cont.yaxis || 'y'] = true;
}
}
undoit[ai][i] = oldVal;
// set the new value - if val is an array, it's one el per trace
// first check for attributes that get more complex alterations
var swapAttrs = [
'swapxy', 'swapxyaxes', 'orientation', 'orientationaxes'
];
if(swapAttrs.indexOf(ai) !== -1) {
// setting an orientation: make sure it's changing
// before we swap everything else
if(ai === 'orientation') {
param.set(newVal);
if(param.get() === undoit[ai][i]) continue;
}
// orientationaxes has no value,
// it flips everything and the axes
else if(ai === 'orientationaxes') {
cont.orientation =
{v: 'h', h: 'v'}[contFull.orientation];
}
helpers.swapXYData(cont);
}
else if(Plots.dataArrayContainers.indexOf(param.parts[0]) !== -1) {
// TODO: use manageArrays.applyContainerArrayChanges here too
helpers.manageArrayContainers(param, newVal, undoit);
flags.docalc = true;
}
else {
var moduleAttrs = (contFull._module || {}).attributes || {};
var valObject = Lib.nestedProperty(moduleAttrs, ai).get() || {};
// if restyling entire attribute container, assume worse case
if(!valObject.valType) {
flags.docalc = true;
}
// must redo calcdata when restyling array values of arrayOk attributes
if(valObject.arrayOk && (Array.isArray(newVal) || Array.isArray(oldVal))) {
flags.docalc = true;
}
// all the other ones, just modify that one attribute
param.set(newVal);
}
}
// swap the data attributes of the relevant x and y axes?
if(['swapxyaxes', 'orientationaxes'].indexOf(ai) !== -1) {
Plotly.Axes.swap(gd, traces);
}
// swap hovermode if set to "compare x/y data"
if(ai === 'orientationaxes') {
var hovermode = Lib.nestedProperty(gd.layout, 'hovermode');
if(hovermode.get() === 'x') {
hovermode.set('y');
} else if(hovermode.get() === 'y') {
hovermode.set('x');
}
}
// check if we need to call axis type
if((traces.indexOf(0) !== -1) && (axtypeAttrs.indexOf(ai) !== -1)) {
Plotly.Axes.clearTypes(gd, traces);
flags.docalc = true;
}
// switching from auto to manual binning or z scaling doesn't
// actually do anything but change what you see in the styling
// box. everything else at least needs to apply styles
if((['autobinx', 'autobiny', 'zauto'].indexOf(ai) === -1) ||
newVal !== false) {
flags.dostyle = true;
}
if(['colorbar', 'line'].indexOf(param.parts[0]) !== -1 ||
param.parts[0] === 'marker' && param.parts[1] === 'colorbar') {
flags.docolorbars = true;
}
var aiArrayStart = ai.indexOf('['),
aiAboveArray = aiArrayStart === -1 ? ai : ai.substr(0, aiArrayStart);
if(recalcAttrs.indexOf(aiAboveArray) !== -1) {
// major enough changes deserve autoscale, autobin, and
// non-reversed axes so people don't get confused
if(['orientation', 'type'].indexOf(ai) !== -1) {
axlist = [];
for(i = 0; i < traces.length; i++) {
var trace = data[traces[i]];
if(Registry.traceIs(trace, 'cartesian')) {
addToAxlist(trace.xaxis || 'x');
addToAxlist(trace.yaxis || 'y');
if(ai === 'type') {
doextra(['autobinx', 'autobiny'], true, i);
}
}
}
doextra(axlist.map(autorangeAttr), true, 0);
doextra(axlist.map(rangeAttr), [0, 1], 0);
}
flags.docalc = true;
} else if(replotAttrs.indexOf(aiAboveArray) !== -1) {
flags.doplot = true;
} else if(aiAboveArray.indexOf('aaxis') === 0 || aiAboveArray.indexOf('baxis') === 0) {
flags.doplot = true;
} else if(autorangeAttrs.indexOf(aiAboveArray) !== -1) {
flags.docalcAutorange = true;
}
}
// do we need to force a recalc?
Plotly.Axes.list(gd).forEach(function(ax) {
if(ax.autorange) flags.autorangeOn = true;
});
// check axes we've flagged for possible deletion
// flagAxForDelete is a hash so we can make sure we only get each axis once
var axListForDelete = Object.keys(flagAxForDelete);
axisLoop:
for(i = 0; i < axListForDelete.length; i++) {
var axId = axListForDelete[i],
axLetter = axId.charAt(0),
axAttr = axLetter + 'axis';
for(var j = 0; j < data.length; j++) {
if(Registry.traceIs(data[j], 'cartesian') &&
(data[j][axAttr] || axLetter) === axId) {
continue axisLoop;
}
}
// no data on this axis - delete it.
doextra('LAYOUT' + Plotly.Axes.id2name(axId), null, 0);
}
// combine a few flags together;
if(flags.docalc || (flags.docalcAutorange && flags.autorangeOn)) {
flags.clearCalc = true;
}
if(flags.docalc || flags.doplot || flags.docalcAutorange) {
flags.fullReplot = true;
}
return {
flags: flags,
undoit: undoit,
redoit: redoit,
traces: traces,
eventData: Lib.extendDeepNoArrays([], [redoit, traces])
};
}
/**
* relayout: update layout attributes of an existing plot
*
* Can be called two ways:
*
* Signature 1:
* @param {String | HTMLDivElement} gd
* the id or dom element of the graph container div
* @param {String} astr
* attribute string (like `'xaxis.range[0]'`) to update
* @param {*} val
* value to give this attribute
*
* Signature 2:
* @param {String | HTMLDivElement} gd
* (as in signature 1)
* @param {Object} aobj
* attribute object `{astr1: val1, astr2: val2 ...}`
* allows setting multiple attributes simultaneously
*/
Plotly.relayout = function relayout(gd, astr, val) {
gd = helpers.getGraphDiv(gd);
helpers.clearPromiseQueue(gd);
if(gd.framework && gd.framework.isPolar) {
return Promise.resolve(gd);
}
var aobj = {};
if(typeof astr === 'string') {
aobj[astr] = val;
} else if(Lib.isPlainObject(astr)) {
aobj = Lib.extendFlat({}, astr);
} else {
Lib.warn('Relayout fail.', astr, val);
return Promise.reject();
}
if(Object.keys(aobj).length) gd.changed = true;
var specs = _relayout(gd, aobj),
flags = specs.flags;
// clear calcdata if required
if(flags.docalc) gd.calcdata = undefined;
// fill in redraw sequence
// even if we don't have anything left in aobj,
// something may have happened within relayout that we
// need to wait for
var seq = [Plots.previousPromises];
if(flags.layoutReplot) {
seq.push(subroutines.layoutReplot);
}
else if(Object.keys(aobj).length) {
Plots.supplyDefaults(gd);
if(flags.dolegend) seq.push(subroutines.doLegend);
if(flags.dolayoutstyle) seq.push(subroutines.layoutStyles);
if(flags.doticks) seq.push(subroutines.doTicksRelayout);
if(flags.domodebar) seq.push(subroutines.doModeBar);
if(flags.docamera) seq.push(subroutines.doCamera);
}
seq.push(Plots.rehover);
Queue.add(gd,
relayout, [gd, specs.undoit],
relayout, [gd, specs.redoit]
);
var plotDone = Lib.syncOrAsync(seq, gd);
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);
return plotDone.then(function() {
gd.emit('plotly_relayout', specs.eventData);
return gd;
});
};
function _relayout(gd, aobj) {
var layout = gd.layout,
fullLayout = gd._fullLayout,
keys = Object.keys(aobj),
axes = Plotly.Axes.list(gd),
arrayEdits = {},
arrayStr,
i,
j;
// look for 'allaxes', split out into all axes
// in case of 3D the axis are nested within a scene which is held in _id
for(i = 0; i < keys.length; i++) {
if(keys[i].indexOf('allaxes') === 0) {
for(j = 0; j < axes.length; j++) {
var scene = axes[j]._id.substr(1),
axisAttr = (scene.indexOf('scene') !== -1) ? (scene + '.') : '',
newkey = keys[i].replace('allaxes', axisAttr + axes[j]._name);
if(!aobj[newkey]) aobj[newkey] = aobj[keys[i]];
}
delete aobj[keys[i]];
}
}
// initialize flags
var flags = {
dolegend: false,
doticks: false,
dolayoutstyle: false,
doplot: false,
docalc: false,
domodebar: false,
docamera: false,
layoutReplot: false
};
// copies of the change (and previous values of anything affected)
// for the undo / redo queue
var redoit = {},
undoit = {};
// for attrs that interact (like scales & autoscales), save the
// old vals before making the change
// val=undefined will not set a value, just record what the value was.
// attr can be an array to set several at once (all to the same val)
function doextra(attr, val) {
if(Array.isArray(attr)) {
attr.forEach(function(a) { doextra(a, val); });
return;
}
// if we have another value for this attribute (explicitly or
// via a parent) do not override with this auto-generated extra
if(attr in aobj || helpers.hasParent(aobj, attr)) return;
var p = Lib.nestedProperty(layout, attr);
if(!(attr in undoit)) undoit[attr] = p.get();
if(val !== undefined) p.set(val);
}
// for editing annotations or shapes - is it on autoscaled axes?
function refAutorange(obj, axLetter) {
if(!Lib.isPlainObject(obj)) return false;
var axRef = obj[axLetter + 'ref'] || axLetter,
ax = Plotly.Axes.getFromId(gd, axRef);
if(!ax && axRef.charAt(0) === axLetter) {
// fall back on the primary axis in case we've referenced a
// nonexistent axis (as we do above if axRef is missing).
// This assumes the object defaults to data referenced, which
// is the case for shapes and annotations but not for images.
// The only thing this is used for is to determine whether to
// do a full `recalc`, so the only ill effect of this error is
// to waste some time.
ax = Plotly.Axes.getFromId(gd, axLetter);
}
return (ax || {}).autorange;
}
// for constraint enforcement: keep track of all axes (as {id: name})
// we're editing the (auto)range of, so we can tell the others constrained
// to scale with them that it's OK for them to shrink
var rangesAltered = {};
function recordAlteredAxis(pleafPlus) {
var axId = axisIds.name2id(pleafPlus.split('.')[0]);
rangesAltered[axId] = 1;
}
// alter gd.layout
for(var ai in aobj) {
if(helpers.hasParent(aobj, ai)) {
throw new Error('cannot set ' + ai + 'and a parent attribute simultaneously');
}
var p = Lib.nestedProperty(layout, ai),
vi = aobj[ai],
plen = p.parts.length,
// p.parts may end with an index integer if the property is an array
pend = typeof p.parts[plen - 1] === 'string' ? (plen - 1) : (plen - 2),
// last property in chain (leaf node)
proot = p.parts[0],
pleaf = p.parts[pend],
// leaf plus immediate parent
pleafPlus = p.parts[pend - 1] + '.' + pleaf,
// trunk nodes (everything except the leaf)
ptrunk = p.parts.slice(0, pend).join('.'),
parentIn = Lib.nestedProperty(gd.layout, ptrunk).get(),
parentFull = Lib.nestedProperty(fullLayout, ptrunk).get();
if(vi === undefined) continue;
redoit[ai] = vi;
// axis reverse is special - it is its own inverse
// op and has no flag.
undoit[ai] = (pleaf === 'reverse') ? vi : p.get();
// Setting width or height to null must reset the graph's width / height
// back to its initial value as computed during the first pass in Plots.plotAutoSize.
//
// To do so, we must manually set them back here using the _initialAutoSize cache.
if(['width', 'height'].indexOf(ai) !== -1 && vi === null) {
fullLayout[ai] = gd._initialAutoSize[ai];
}
// check autorange vs range
else if(pleafPlus.match(/^[xyz]axis[0-9]*\.range(\[[0|1]\])?$/)) {
doextra(ptrunk + '.autorange', false);
recordAlteredAxis(pleafPlus);
}
else if(pleafPlus.match(/^[xyz]axis[0-9]*\.autorange$/)) {
doextra([ptrunk + '.range[0]', ptrunk + '.range[1]'],
undefined);
recordAlteredAxis(pleafPlus);
}
else if(pleafPlus.match(/^aspectratio\.[xyz]$/)) {
doextra(proot + '.aspectmode', 'manual');
}
else if(pleafPlus.match(/^aspectmode$/)) {
doextra([ptrunk + '.x', ptrunk + '.y', ptrunk + '.z'], undefined);
}
else if(pleaf === 'tick0' || pleaf === 'dtick') {
doextra(ptrunk + '.tickmode', 'linear');
}
else if(pleaf === 'tickmode') {
doextra([ptrunk + '.tick0', ptrunk + '.dtick'], undefined);
}
else if(/[xy]axis[0-9]*?$/.test(pleaf) && !Object.keys(vi || {}).length) {
flags.docalc = true;
}
else if(/[xy]axis[0-9]*\.categoryorder$/.test(pleafPlus)) {
flags.docalc = true;
}
else if(/[xy]axis[0-9]*\.categoryarray/.test(pleafPlus)) {
flags.docalc = true;
}
if(pleafPlus.indexOf('rangeslider') !== -1) {
flags.docalc = true;
}
// toggling axis type between log and linear: we need to convert
// positions for components that are still using linearized values,
// not data values like newer components.
// previously we did this for log <-> not-log, but now only do it
// for log <-> linear
if(pleaf === 'type') {
var ax = parentIn,
toLog = parentFull.type === 'linear' && vi === 'log',
fromLog = parentFull.type === 'log' && vi === 'linear';
if(toLog || fromLog) {
if(!ax || !ax.range) {
doextra(ptrunk + '.autorange', true);
}
else if(!parentFull.autorange) {
// toggling log without autorange: need to also recalculate ranges
// because log axes use linearized values for range endpoints
var r0 = ax.range[0],
r1 = ax.range[1];
if(toLog) {
// if both limits are negative, autorange
if(r0 <= 0 && r1 <= 0) {
doextra(ptrunk + '.autorange', true);
}
// if one is negative, set it 6 orders below the other.
if(r0 <= 0) r0 = r1 / 1e6;
else if(r1 <= 0) r1 = r0 / 1e6;
// now set the range values as appropriate
doextra(ptrunk + '.range[0]', Math.log(r0) / Math.LN10);
doextra(ptrunk + '.range[1]', Math.log(r1) / Math.LN10);
}
else {
doextra(ptrunk + '.range[0]', Math.pow(10, r0));
doextra(ptrunk + '.range[1]', Math.pow(10, r1));
}
}
else if(toLog) {
// just make sure the range is positive and in the right
// order, it'll get recalculated later
ax.range = (ax.range[1] > ax.range[0]) ? [1, 2] : [2, 1];
}
// Annotations and images also need to convert to/from linearized coords
// Shapes do not need this :)
Registry.getComponentMethod('annotations', 'convertCoords')(gd, parentFull, vi, doextra);
Registry.getComponentMethod('images', 'convertCoords')(gd, parentFull, vi, doextra);
}
else {
// any other type changes: the range from the previous type
// will not make sense, so autorange it.
doextra(ptrunk + '.autorange', true);
}
}
else if(pleaf.match(cartesianConstants.AX_NAME_PATTERN)) {
var fullProp = Lib.nestedProperty(fullLayout, ai).get(),
newType = (vi || {}).type;
// This can potentially cause strange behavior if the autotype is not
// numeric (linear, because we don't auto-log) but the previous type
// was log. That's a very strange edge case though
if(!newType || newType === '-') newType = 'linear';
Registry.getComponentMethod('annotations', 'convertCoords')(gd, fullProp, newType, doextra);
Registry.getComponentMethod('images', 'convertCoords')(gd, fullProp, newType, doextra);
}
// alter gd.layout
// collect array component edits for execution all together
// so we can ensure consistent behavior adding/removing items
// and order-independence for add/remove/edit all together in
// one relayout call
var containerArrayMatch = manageArrays.containerArrayMatch(ai);
if(containerArrayMatch) {
arrayStr = containerArrayMatch.array;
i = containerArrayMatch.index;
var propStr = containerArrayMatch.property,
componentArray = Lib.nestedProperty(layout, arrayStr),
obji = (componentArray || [])[i] || {};
if(i === '') {
// replacing the entire array: too much going on, force recalc
if(ai.indexOf('updatemenus') === -1) flags.docalc = true;
}
else if(propStr === '') {
// special handling of undoit if we're adding or removing an element
// ie 'annotations[2]' which can be {...} (add) or null (remove)
var toggledObj = vi;
if(manageArrays.isAddVal(vi)) {
undoit[ai] = null;
}
else if(manageArrays.isRemoveVal(vi)) {
undoit[ai] = obji;
toggledObj = obji;
}
else Lib.warn('unrecognized full object value', aobj);
if(refAutorange(toggledObj, 'x') || refAutorange(toggledObj, 'y') &&
ai.indexOf('updatemenus') === -1) {
flags.docalc = true;
}
}
else if((refAutorange(obji, 'x') || refAutorange(obji, 'y')) &&
!Lib.containsAny(ai, ['color', 'opacity', 'align', 'dash', 'updatemenus'])) {
flags.docalc = true;
}
// prepare the edits object we'll send to applyContainerArrayChanges
if(!arrayEdits[arrayStr]) arrayEdits[arrayStr] = {};
var objEdits = arrayEdits[arrayStr][i];
if(!objEdits) objEdits = arrayEdits[arrayStr][i] = {};
objEdits[propStr] = vi;
delete aobj[ai];
}
// handle axis reversal explicitly, as there's no 'reverse' flag
else if(pleaf === 'reverse') {
if(parentIn.range) parentIn.range.reverse();
else {
doextra(ptrunk + '.autorange', true);
parentIn.range = [1, 0];
}
if(parentFull.autorange) flags.docalc = true;
else flags.doplot = true;
}
else {
var pp1 = String(p.parts[1] || '');
// check whether we can short-circuit a full redraw
// 3d or geo at this point just needs to redraw.
if(proot.indexOf('scene') === 0) {
if(p.parts[1] === 'camera') flags.docamera = true;
else flags.doplot = true;
}
else if(proot.indexOf('geo') === 0) flags.doplot = true;
else if(proot.indexOf('ternary') === 0) flags.doplot = true;
else if(ai === 'paper_bgcolor') flags.doplot = true;
else if(proot === 'margin' ||
pp1 === 'autorange' ||
pp1 === 'rangemode' ||
pp1 === 'type' ||
pp1 === 'domain' ||
pp1 === 'fixedrange' ||
pp1 === 'scaleanchor' ||
pp1 === 'scaleratio' ||
ai.indexOf('calendar') !== -1 ||
ai.match(/^(bar|box|font)/)) {
flags.docalc = true;
}
else if(fullLayout._has('gl2d') &&
(ai.indexOf('axis') !== -1 || ai === 'plot_bgcolor')
) flags.doplot = true;
else if(ai === 'hiddenlabels') flags.docalc = true;
else if(proot.indexOf('legend') !== -1) flags.dolegend = true;
else if(ai.indexOf('title') !== -1) flags.doticks = true;
else if(proot.indexOf('bgcolor') !== -1) flags.dolayoutstyle = true;
else if(plen > 1 && Lib.containsAny(pp1, ['tick', 'exponent', 'grid', 'zeroline'])) {
flags.doticks = true;
}
else if(ai.indexOf('.linewidth') !== -1 &&
ai.indexOf('axis') !== -1) {
flags.doticks = flags.dolayoutstyle = true;
}
else if(plen > 1 && pp1.indexOf('line') !== -1) {
flags.dolayoutstyle = true;
}
else if(plen > 1 && pp1 === 'mirror') {
flags.doticks = flags.dolayoutstyle = true;
}
else if(ai === 'margin.pad') {
flags.doticks = flags.dolayoutstyle = true;
}
/*
* hovermode, dragmode, and spikes don't need any redrawing, since they just
* affect reaction to user input. Everything else, assume full replot.
* height, width, autosize get dealt with below. Except for the case of
* of subplots - scenes - which require scene.updateFx to be called.
*/
else if(['hovermode', 'dragmode'].indexOf(ai) !== -1 ||
ai.indexOf('spike') !== -1) {
flags.domodebar = true;
}
else if(['height', 'width', 'autosize'].indexOf(ai) === -1) {
flags.doplot = true;
}
p.set(vi);
}
}
// now we've collected component edits - execute them all together
for(arrayStr in arrayEdits) {
var finished = manageArrays.applyContainerArrayChanges(gd,
Lib.nestedProperty(layout, arrayStr), arrayEdits[arrayStr], flags);
if(!finished) flags.doplot = true;
}
// figure out if we need to recalculate axis constraints
var constraints = fullLayout._axisConstraintGroups;
for(var axId in rangesAltered) {
for(i = 0; i < constraints.length; i++) {
var group = constraints[i];
if(group[axId]) {
// Always recalc if we're changing constrained ranges.
// Otherwise it's possible to violate the constraints by
// specifying arbitrary ranges for all axes in the group.
// this way some ranges may expand beyond what's specified,
// as they do at first draw, to satisfy the constraints.
flags.docalc = true;
for(var groupAxId in group) {
if(!rangesAltered[groupAxId]) {
axisIds.getFromId(gd, groupAxId)._constraintShrinkable = true;
}
}
}
}
}
var oldWidth = fullLayout.width,
oldHeight = fullLayout.height;
// calculate autosizing
if(gd.layout.autosize) Plots.plotAutoSize(gd, gd.layout, fullLayout);
// avoid unnecessary redraws
var hasSizechanged = aobj.height || aobj.width ||
(fullLayout.width !== oldWidth) ||
(fullLayout.height !== oldHeight);
if(hasSizechanged) flags.docalc = true;
if(flags.doplot || flags.docalc) {
flags.layoutReplot = true;
}
// now all attribute mods are done, as are
// redo and undo so we can save them
return {
flags: flags,
undoit: undoit,
redoit: redoit,
eventData: Lib.extendDeep({}, redoit)
};
}
/**
* update: update trace and layout attributes of an existing plot
*
* @param {String | HTMLDivElement} gd
* the id or DOM element of the graph container div
* @param {Object} traceUpdate
* attribute object `{astr1: val1, astr2: val2 ...}`
* corresponding to updates in the plot's traces
* @param {Object} layoutUpdate
* attribute object `{astr1: val1, astr2: val2 ...}`
* corresponding to updates in the plot's layout
* @param {Number[] | Number} [traces]
* integer or array of integers for the traces to alter (all if omitted)
*
*/
Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
gd = helpers.getGraphDiv(gd);
helpers.clearPromiseQueue(gd);
if(gd.framework && gd.framework.isPolar) {
return Promise.resolve(gd);
}
if(!Lib.isPlainObject(traceUpdate)) traceUpdate = {};
if(!Lib.isPlainObject(layoutUpdate)) layoutUpdate = {};
if(Object.keys(traceUpdate).length) gd.changed = true;
if(Object.keys(layoutUpdate).length) gd.changed = true;
var restyleSpecs = _restyle(gd, Lib.extendFlat({}, traceUpdate), traces),
restyleFlags = restyleSpecs.flags;
var relayoutSpecs = _relayout(gd, Lib.extendFlat({}, layoutUpdate)),
relayoutFlags = relayoutSpecs.flags;
// clear calcdata if required
if(restyleFlags.clearCalc || relayoutFlags.docalc) gd.calcdata = undefined;
// fill in redraw sequence
var seq = [];
if(restyleFlags.fullReplot && relayoutFlags.layoutReplot) {
var data = gd.data,
layout = gd.layout;
// clear existing data/layout on gd
// so that Plotly.plot doesn't try to extend them
gd.data = undefined;
gd.layout = undefined;
seq.push(function() { return Plotly.plot(gd, data, layout); });
}
else if(restyleFlags.fullReplot) {
seq.push(Plotly.plot);
}
else if(relayoutFlags.layoutReplot) {
seq.push(subroutines.layoutReplot);
}
else {
seq.push(Plots.previousPromises);
Plots.supplyDefaults(gd);
if(restyleFlags.dostyle) seq.push(subroutines.doTraceStyle);
if(restyleFlags.docolorbars) seq.push(subroutines.doColorBars);
if(relayoutFlags.dolegend) seq.push(subroutines.doLegend);
if(relayoutFlags.dolayoutstyle) seq.push(subroutines.layoutStyles);
if(relayoutFlags.doticks) seq.push(subroutines.doTicksRelayout);
if(relayoutFlags.domodebar) seq.push(subroutines.doModeBar);
if(relayoutFlags.doCamera) seq.push(subroutines.doCamera);
}
seq.push(Plots.rehover);
Queue.add(gd,
update, [gd, restyleSpecs.undoit, relayoutSpecs.undoit, restyleSpecs.traces],
update, [gd, restyleSpecs.redoit, relayoutSpecs.redoit, restyleSpecs.traces]
);
var plotDone = Lib.syncOrAsync(seq, gd);
if(!plotDone || !plotDone.then) plotDone = Promise.resolve(gd);
return plotDone.then(function() {
gd.emit('plotly_update', {
data: restyleSpecs.eventData,
layout: relayoutSpecs.eventData
});
return gd;
});
};
/**
* Animate to a frame, sequence of frame, frame group, or frame definition
*
* @param {string id or DOM element} gd
* the id or DOM element of the graph container div
*
* @param {string or object or array of strings or array of objects} frameOrGroupNameOrFrameList
* a single frame, array of frames, or group to which to animate. The intent is
* inferred by the type of the input. Valid inputs are:
*
* - string, e.g. 'groupname': animate all frames of a given `group` in the order
* in which they are defined via `Plotly.addFrames`.
*
* - array of strings, e.g. ['frame1', frame2']: a list of frames by name to which
* to animate in sequence
*
* - object: {data: ...}: a frame definition to which to animate. The frame is not
* and does not need to be added via `Plotly.addFrames`. It may contain any of
* the properties of a frame, including `data`, `layout`, and `traces`. The
* frame is used as provided and does not use the `baseframe` property.
*
* - array of objects, e.g. [{data: ...}, {data: ...}]: a list of frame objects,
* each following the same rules as a single `object`.
*
* @param {object} animationOpts
* configuration for the animation
*/
Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
gd = helpers.getGraphDiv(gd);
if(!Lib.isPlotDiv(gd)) {
throw new Error(
'This element is not a Plotly plot: ' + gd + '. It\'s likely that you\'ve failed ' +
'to create a plot before animating it. For more details, see ' +
'https://plot.ly/javascript/animations/'
);
}
var trans = gd._transitionData;
// This is the queue of frames that will be animated as soon as possible. They
// are popped immediately upon the *start* of a transition:
if(!trans._frameQueue) {
trans._frameQueue = [];
}
animationOpts = Plots.supplyAnimationDefaults(animationOpts);
var transitionOpts = animationOpts.transition;
var frameOpts = animationOpts.frame;
// Since frames are popped immediately, an empty queue only means all frames have
// *started* to transition, not that the animation is complete. To solve that,
// track a separate counter that increments at the same time as frames are added
// to the queue, but decrements only when the transition is complete.
if(trans._frameWaitingCnt === undefined) {
trans._frameWaitingCnt = 0;
}
function getTransitionOpts(i) {
if(Array.isArray(transitionOpts)) {
if(i >= transitionOpts.length) {
return transitionOpts[0];
} else {
return transitionOpts[i];
}
} else {
return transitionOpts;
}
}
function getFrameOpts(i) {
if(Array.isArray(frameOpts)) {
if(i >= frameOpts.length) {
return frameOpts[0];
} else {
return frameOpts[i];
}
} else {
return frameOpts;
}
}
// Execute a callback after the wrapper function has been called n times.
// This is used to defer the resolution until a transition has resovled *and*
// the frame has completed. If it's not done this way, then we get a race
// condition in which the animation might resolve before a transition is complete
// or vice versa.
function callbackOnNthTime(cb, n) {
var cnt = 0;
return function() {
if(cb && ++cnt === n) {
return cb();
}
};
}
return new Promise(function(resolve, reject) {
function discardExistingFrames() {
if(trans._frameQueue.length === 0) {
return;
}
while(trans._frameQueue.length) {
var next = trans._frameQueue.pop();
if(next.onInterrupt) {
next.onInterrupt();
}
}
gd.emit('plotly_animationinterrupted', []);
}
function queueFrames(frameList) {
if(frameList.length === 0) return;
for(var i = 0; i < frameList.length; i++) {
var computedFrame;
if(frameList[i].type === 'byname') {
// If it's a named frame, compute it:
computedFrame = Plots.computeFrame(gd, frameList[i].name);
} else {
// Otherwise we must have been given a simple object, so treat
// the input itself as the computed frame.
computedFrame = frameList[i].data;
}
var frameOpts = getFrameOpts(i);
var transitionOpts = getTransitionOpts(i);
// It doesn't make much sense for the transition duration to be greater than
// the frame duration, so limit it:
transitionOpts.duration = Math.min(transitionOpts.duration, frameOpts.duration);
var nextFrame = {
frame: computedFrame,
name: frameList[i].name,
frameOpts: frameOpts,
transitionOpts: transitionOpts,
};
if(i === frameList.length - 1) {
// The last frame in this .animate call stores the promise resolve
// and reject callbacks. This is how we ensure that the animation
// loop (which may exist as a result of a *different* .animate call)
// still resolves or rejecdts this .animate call's promise. once it's
// complete.
nextFrame.onComplete = callbackOnNthTime(resolve, 2);
nextFrame.onInterrupt = reject;
}
trans._frameQueue.push(nextFrame);
}
// Set it as never having transitioned to a frame. This will cause the animation
// loop to immediately transition to the next frame (which, for immediate mode,
// is the first frame in the list since all others would have been discarded
// below)
if(animationOpts.mode === 'immediate') {
trans._lastFrameAt = -Infinity;
}
// Only it's not already running, start a RAF loop. This could be avoided in the
// case that there's only one frame, but it significantly complicated the logic
// and only sped things up by about 5% or so for a lorenz attractor simulation.
// It would be a fine thing to implement, but the benefit of that optimization
// doesn't seem worth the extra complexity.
if(!trans._animationRaf) {
beginAnimationLoop();
}
}
function stopAnimationLoop() {
gd.emit('plotly_animated');
// Be sure to unset also since it's how we know whether a loop is already running:
window.cancelAnimationFrame(trans._animationRaf);
trans._animationRaf = null;
}
function nextFrame() {
if(trans._currentFrame && trans._currentFrame.onComplete) {
// Execute the callback and unset it to ensure it doesn't
// accidentally get called twice
trans._currentFrame.onComplete();
}
var newFrame = trans._currentFrame = trans._frameQueue.shift();
if(newFrame) {
// Since it's sometimes necessary to do deep digging into frame data,
// we'll consider it not 100% impossible for nulls or numbers to sneak through,
// so check when casting the name, just to be absolutely certain:
var stringName = newFrame.name ? newFrame.name.toString() : null;
gd._fullLayout._currentFrame = stringName;
trans._lastFrameAt = Date.now();
trans._timeToNext = newFrame.frameOpts.duration;
// This is simply called and it's left to .transition to decide how to manage
// interrupting current transitions. That means we don't need to worry about
// how it resolves or what happens after this:
Plots.transition(gd,
newFrame.frame.data,
newFrame.frame.layout,
helpers.coerceTraceIndices(gd, newFrame.frame.traces),
newFrame.frameOpts,
newFrame.transitionOpts
).then(function() {
if(newFrame.onComplete) {
newFrame.onComplete();
}
});
gd.emit('plotly_animatingframe', {
name: stringName,
frame: newFrame.frame,
animation: {
frame: newFrame.frameOpts,
transition: newFrame.transitionOpts,
}
});
} else {
// If there are no more frames, then stop the RAF loop:
stopAnimationLoop();
}
}
function beginAnimationLoop() {
gd.emit('plotly_animating');
// If no timer is running, then set last frame = long ago so that the next
// frame is immediately transitioned:
trans._lastFrameAt = -Infinity;
trans._timeToNext = 0;
trans._runningTransitions = 0;
trans._currentFrame = null;
var doFrame = function() {
// This *must* be requested before nextFrame since nextFrame may decide
// to cancel it if there's nothing more to animated:
trans._animationRaf = window.requestAnimationFrame(doFrame);
// Check if we're ready for a new frame:
if(Date.now() - trans._lastFrameAt > trans._timeToNext) {
nextFrame();
}
};
doFrame();
}
// This is an animate-local counter that helps match up option input list
// items with the particular frame.
var configCounter = 0;
function setTransitionConfig(frame) {
if(Array.isArray(transitionOpts)) {
if(configCounter >= transitionOpts.length) {
frame.transitionOpts = transitionOpts[configCounter];
} else {
frame.transitionOpts = transitionOpts[0];
}
} else {
frame.transitionOpts = transitionOpts;
}
configCounter++;
return frame;
}
// Disambiguate what's sort of frames have been received
var i, frame;
var frameList = [];
var allFrames = frameOrGroupNameOrFrameList === undefined || frameOrGroupNameOrFrameList === null;
var isFrameArray = Array.isArray(frameOrGroupNameOrFrameList);
var isSingleFrame = !allFrames && !isFrameArray && Lib.isPlainObject(frameOrGroupNameOrFrameList);
if(isSingleFrame) {
// In this case, a simple object has been passed to animate.
frameList.push({
type: 'object',
data: setTransitionConfig(Lib.extendFlat({}, frameOrGroupNameOrFrameList))
});
} else if(allFrames || ['string', 'number'].indexOf(typeof frameOrGroupNameOrFrameList) !== -1) {
// In this case, null or undefined has been passed so that we want to
// animate *all* currently defined frames
for(i = 0; i < trans._frames.length; i++) {
frame = trans._frames[i];
if(!frame) continue;
if(allFrames || String(frame.group) === String(frameOrGroupNameOrFrameList)) {
frameList.push({
type: 'byname',
name: String(frame.name),
data: setTransitionConfig({name: frame.name})
});
}
}
} else if(isFrameArray) {
for(i = 0; i < frameOrGroupNameOrFrameList.length; i++) {
var frameOrName = frameOrGroupNameOrFrameList[i];
if(['number', 'string'].indexOf(typeof frameOrName) !== -1) {
frameOrName = String(frameOrName);
// In this case, there's an array and this frame is a string name:
frameList.push({
type: 'byname',
name: frameOrName,
data: setTransitionConfig({name: frameOrName})
});
} else if(Lib.isPlainObject(frameOrName)) {
frameList.push({
type: 'object',
data: setTransitionConfig(Lib.extendFlat({}, frameOrName))
});
}
}
}
// Verify that all of these frames actually exist; return and reject if not:
for(i = 0; i < frameList.length; i++) {
frame = frameList[i];
if(frame.type === 'byname' && !trans._frameHash[frame.data.name]) {
Lib.warn('animate failure: frame not found: "' + frame.data.name + '"');
reject();
return;
}
}
// If the mode is either next or immediate, then all currently queued frames must
// be dumped and the corresponding .animate promises rejected.
if(['next', 'immediate'].indexOf(animationOpts.mode) !== -1) {
discardExistingFrames();
}
if(animationOpts.direction === 'reverse') {
frameList.reverse();
}
var currentFrame = gd._fullLayout._currentFrame;
if(currentFrame && animationOpts.fromcurrent) {
var idx = -1;
for(i = 0; i < frameList.length; i++) {
frame = frameList[i];
if(frame.type === 'byname' && frame.name === currentFrame) {
idx = i;
break;
}
}
if(idx > 0 && idx < frameList.length - 1) {
var filteredFrameList = [];
for(i = 0; i < frameList.length; i++) {
frame = frameList[i];
if(frameList[i].type !== 'byname' || i > idx) {
filteredFrameList.push(frame);
}
}
frameList = filteredFrameList;
}
}
if(frameList.length > 0) {
queueFrames(frameList);
} else {
// This is the case where there were simply no frames. It's a little strange
// since there's not much to do:
gd.emit('plotly_animated');
resolve();
}
});
};
/**
* Register new frames
*
* @param {string id or DOM element} gd
* the id or DOM element of the graph container div
*
* @param {array of objects} frameList
* list of frame definitions, in which each object includes any of:
* - name: {string} name of frame to add
* - data: {array of objects} trace data
* - layout {object} layout definition
* - traces {array} trace indices
* - baseframe {string} name of frame from which this frame gets defaults
*
* @param {array of integers) indices
* an array of integer indices matching the respective frames in `frameList`. If not
* provided, an index will be provided in serial order. If already used, the frame
* will be overwritten.
*/
Plotly.addFrames = function(gd, frameList, indices) {
gd = helpers.getGraphDiv(gd);
var numericNameWarningCount = 0;
if(frameList === null || frameList === undefined) {
return Promise.resolve();
}
if(!Lib.isPlotDiv(gd)) {
throw new Error(
'This element is not a Plotly plot: ' + gd + '. It\'s likely that you\'ve failed ' +
'to create a plot before adding frames. For more details, see ' +
'https://plot.ly/javascript/animations/'
);
}
var i, frame, j, idx;
var _frames = gd._transitionData._frames;
var _hash = gd._transitionData._frameHash;
if(!Array.isArray(frameList)) {
throw new Error('addFrames failure: frameList must be an Array of frame definitions' + frameList);
}
// Create a sorted list of insertions since we run into lots of problems if these
// aren't in ascending order of index:
//
// Strictly for sorting. Make sure this is guaranteed to never collide with any
// already-exisisting indices:
var bigIndex = _frames.length + frameList.length * 2;
var insertions = [];
for(i = frameList.length - 1; i >= 0; i--) {
if(!Lib.isPlainObject(frameList[i])) continue;
var name = (_hash[frameList[i].name] || {}).name;
var newName = frameList[i].name;
if(name && newName && typeof newName === 'number' && _hash[name]) {
numericNameWarningCount++;
Lib.warn('addFrames: overwriting frame "' + _hash[name].name +
'" with a frame whose name of type "number" also equates to "' +
name + '". This is valid but may potentially lead to unexpected ' +
'behavior since all plotly.js frame names are stored internally ' +
'as strings.');
if(numericNameWarningCount > 5) {
Lib.warn('addFrames: This API call has yielded too many warnings. ' +
'For the rest of this call, further warnings about numeric frame ' +
'names will be suppressed.');
}
}
insertions.push({
frame: Plots.supplyFrameDefaults(frameList[i]),
index: (indices && indices[i] !== undefined && indices[i] !== null) ? indices[i] : bigIndex + i
});
}
// Sort this, taking note that undefined insertions end up at the end:
insertions.sort(function(a, b) {
if(a.index > b.index) return -1;
if(a.index < b.index) return 1;
return 0;
});
var ops = [];
var revops = [];
var frameCount = _frames.length;
for(i = insertions.length - 1; i >= 0; i--) {
frame = insertions[i].frame;
if(typeof frame.name === 'number') {
Lib.warn('Warning: addFrames accepts frames with numeric names, but the numbers are' +
'implicitly cast to strings');
}
if(!frame.name) {
// Repeatedly assign a default name, incrementing the counter each time until
// we get a name that's not in the hashed lookup table:
while(_hash[(frame.name = 'frame ' + gd._transitionData._counter++)]);
}
if(_hash[frame.name]) {
// If frame is present, overwrite its definition:
for(j = 0; j < _frames.length; j++) {
if((_frames[j] || {}).name === frame.name) break;
}
ops.push({type: 'replace', index: j, value: frame});
revops.unshift({type: 'replace', index: j, value: _frames[j]});
} else {
// Otherwise insert it at the end of the list:
idx = Math.max(0, Math.min(insertions[i].index, frameCount));
ops.push({type: 'insert', index: idx, value: frame});
revops.unshift({type: 'delete', index: idx});
frameCount++;
}
}
var undoFunc = Plots.modifyFrames,
redoFunc = Plots.modifyFrames,
undoArgs = [gd, revops],
redoArgs = [gd, ops];
if(Queue) Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return Plots.modifyFrames(gd, ops);
};
/**
* Delete frame
*
* @param {string id or DOM element} gd
* the id or DOM element of the graph container div
*
* @param {array of integers} frameList
* list of integer indices of frames to be deleted
*/
Plotly.deleteFrames = function(gd, frameList) {
gd = helpers.getGraphDiv(gd);
if(!Lib.isPlotDiv(gd)) {
throw new Error('This element is not a Plotly plot: ' + gd);
}
var i, idx;
var _frames = gd._transitionData._frames;
var ops = [];
var revops = [];
if(!frameList) {
frameList = [];
for(i = 0; i < _frames.length; i++) {
frameList.push(i);
}
}
frameList = frameList.slice(0);
frameList.sort();
for(i = frameList.length - 1; i >= 0; i--) {
idx = frameList[i];
ops.push({type: 'delete', index: idx});
revops.unshift({type: 'insert', index: idx, value: _frames[idx]});
}
var undoFunc = Plots.modifyFrames,
redoFunc = Plots.modifyFrames,
undoArgs = [gd, revops],
redoArgs = [gd, ops];
if(Queue) Queue.add(gd, undoFunc, undoArgs, redoFunc, redoArgs);
return Plots.modifyFrames(gd, ops);
};
/**
* Purge a graph container div back to its initial pre-Plotly.plot state
*
* @param {string id or DOM element} gd
* the id or DOM element of the graph container div
*/
Plotly.purge = function purge(gd) {
gd = helpers.getGraphDiv(gd);
var fullLayout = gd._fullLayout || {},
fullData = gd._fullData || [];
// remove gl contexts
Plots.cleanPlot([], {}, fullData, fullLayout);
// purge properties
Plots.purge(gd);
// purge event emitter methods
Events.purge(gd);
// remove plot container
if(fullLayout._container) fullLayout._container.remove();
delete gd._context;
delete gd._replotPending;
delete gd._mouseDownTime;
delete gd._legendMouseDownTime;
delete gd._hmpixcount;
delete gd._hmlumcount;
return gd;
};
// -------------------------------------------------------
// makePlotFramework: Create the plot container and axes
// -------------------------------------------------------
function makePlotFramework(gd) {
var gd3 = d3.select(gd),
fullLayout = gd._fullLayout;
// Plot container
fullLayout._container = gd3.selectAll('.plot-container').data([0]);
fullLayout._container.enter().insert('div', ':first-child')
.classed('plot-container', true)
.classed('plotly', true);
// Make the svg container
fullLayout._paperdiv = fullLayout._container.selectAll('.svg-container').data([0]);
fullLayout._paperdiv.enter().append('div')
.classed('svg-container', true)
.style('position', 'relative');
// Make the graph containers
// start fresh each time we get here, so we know the order comes out
// right, rather than enter/exit which can muck up the order
// TODO: sort out all the ordering so we don't have to
// explicitly delete anything
fullLayout._glcontainer = fullLayout._paperdiv.selectAll('.gl-container')
.data([0]);
fullLayout._glcontainer.enter().append('div')
.classed('gl-container', true);
fullLayout._paperdiv.selectAll('.main-svg').remove();
fullLayout._paper = fullLayout._paperdiv.insert('svg', ':first-child')
.classed('main-svg', true);
fullLayout._toppaper = fullLayout._paperdiv.append('svg')
.classed('main-svg', true);
if(!fullLayout._uid) {
var otherUids = [];
d3.selectAll('defs').each(function() {
if(this.id) otherUids.push(this.id.split('-')[1]);
});
fullLayout._uid = Lib.randstr(otherUids);
}
fullLayout._paperdiv.selectAll('.main-svg')
.attr(xmlnsNamespaces.svgAttrs);
fullLayout._defs = fullLayout._paper.append('defs')
.attr('id', 'defs-' + fullLayout._uid);
fullLayout._topdefs = fullLayout._toppaper.append('defs')
.attr('id', 'topdefs-' + fullLayout._uid);
fullLayout._bgLayer = fullLayout._paper.append('g')
.classed('bglayer', true);
fullLayout._draggers = fullLayout._paper.append('g')
.classed('draglayer', true);
// lower shape/image layer - note that this is behind
// all subplots data/grids but above the backgrounds
// except inset subplots, whose backgrounds are drawn
// inside their own group so that they appear above
// the data for the main subplot
// lower shapes and images which are fully referenced to
// a subplot still get drawn within the subplot's group
// so they will work correctly on insets
var layerBelow = fullLayout._paper.append('g')
.classed('layer-below', true);
fullLayout._imageLowerLayer = layerBelow.append('g')
.classed('imagelayer', true);
fullLayout._shapeLowerLayer = layerBelow.append('g')
.classed('shapelayer', true);
// single cartesian layer for the whole plot
fullLayout._cartesianlayer = fullLayout._paper.append('g').classed('cartesianlayer', true);
// single ternary layer for the whole plot
fullLayout._ternarylayer = fullLayout._paper.append('g').classed('ternarylayer', true);
// single geo layer for the whole plot
fullLayout._geolayer = fullLayout._paper.append('g').classed('geolayer', true);
// upper shape layer
// (only for shapes to be drawn above the whole plot, including subplots)
var layerAbove = fullLayout._paper.append('g')
.classed('layer-above', true);
fullLayout._imageUpperLayer = layerAbove.append('g')
.classed('imagelayer', true);
fullLayout._shapeUpperLayer = layerAbove.append('g')
.classed('shapelayer', true);
// single pie layer for the whole plot
fullLayout._pielayer = fullLayout._paper.append('g').classed('pielayer', true);
// fill in image server scrape-svg
fullLayout._glimages = fullLayout._paper.append('g').classed('glimages', true);
// lastly info (legend, annotations) and hover layers go on top
// these are in a different svg element normally, but get collapsed into a single
// svg when exporting (after inserting 3D)
fullLayout._infolayer = fullLayout._toppaper.append('g').classed('infolayer', true);
fullLayout._zoomlayer = fullLayout._toppaper.append('g').classed('zoomlayer', true);
fullLayout._hoverlayer = fullLayout._toppaper.append('g').classed('hoverlayer', true);
gd.emit('plotly_framework');
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/* eslint-disable no-console */
/**
* This will be transferred over to gd and overridden by
* config args to Plotly.plot.
*
* The defaults are the appropriate settings for plotly.js,
* so we get the right experience without any config argument.
*/
module.exports = {
// no interactivity, for export or image generation
staticPlot: false,
// we can edit titles, move annotations, etc
editable: false,
// DO autosize once regardless of layout.autosize
// (use default width or height values otherwise)
autosizable: false,
// set the length of the undo/redo queue
queueLength: 0,
// if we DO autosize, do we fill the container or the screen?
fillFrame: false,
// if we DO autosize, set the frame margins in percents of plot size
frameMargins: 0,
// mousewheel or two-finger scroll zooms the plot
scrollZoom: false,
// double click interaction (false, 'reset', 'autosize' or 'reset+autosize')
doubleClick: 'reset+autosize',
// new users see some hints about interactivity
showTips: true,
// enable axis pan/zoom drag handles
showAxisDragHandles: true,
// enable direct range entry at the pan/zoom drag points (drag handles must be enabled above)
showAxisRangeEntryBoxes: true,
// link to open this plot in plotly
showLink: false,
// if we show a link, does it contain data or just link to a plotly file?
sendData: true,
// text appearing in the sendData link
linkText: 'Edit chart',
// false or function adding source(s) to linkText <text>
showSources: false,
// display the mode bar (true, false, or 'hover')
displayModeBar: 'hover',
// remove mode bar button by name
// (see ./components/modebar/buttons.js for the list of names)
modeBarButtonsToRemove: [],
// add mode bar button using config objects
// (see ./components/modebar/buttons.js for list of arguments)
modeBarButtonsToAdd: [],
// fully custom mode bar buttons as nested array,
// where the outer arrays represents button groups, and
// the inner arrays have buttons config objects or names of default buttons
// (see ./components/modebar/buttons.js for more info)
modeBarButtons: false,
// add the plotly logo on the end of the mode bar
displaylogo: true,
// increase the pixel ratio for Gl plot images
plotGlPixelRatio: 2,
// function to add the background color to a different container
// or 'opaque' to ensure there's white behind it
setBackground: defaultSetBackground,
// URL to topojson files used in geo charts
topojsonURL: 'https://cdn.plot.ly/',
// Mapbox access token (required to plot mapbox trace types)
// If using an Mapbox Atlas server, set this option to '',
// so that plotly.js won't attempt to authenticate to the public Mapbox server.
mapboxAccessToken: null,
// Turn all console logging on or off (errors will be thrown)
// This should ONLY be set via Plotly.setPlotConfig
logging: false,
// Set global transform to be applied to all traces with no
// specification needed
globalTransforms: []
};
// where and how the background gets set can be overridden by context
// so we define the default (plotly.js) behavior here
function defaultSetBackground(gd, bgColor) {
try {
gd._fullLayout._paper.style('background', bgColor);
}
catch(e) {
if(module.exports.logging > 0) {
console.error(e);
}
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | 12 12 12 12 12 12 12 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../registry');
var Lib = require('../lib');
var baseAttributes = require('../plots/attributes');
var baseLayoutAttributes = require('../plots/layout_attributes');
var frameAttributes = require('../plots/frame_attributes');
var animationAttributes = require('../plots/animation_attributes');
// polar attributes are not part of the Registry yet
var polarAreaAttrs = require('../plots/polar/area_attributes');
var polarAxisAttrs = require('../plots/polar/axis_attributes');
var extendFlat = Lib.extendFlat;
var extendDeep = Lib.extendDeep;
var IS_SUBPLOT_OBJ = '_isSubplotObj';
var IS_LINKED_TO_ARRAY = '_isLinkedToArray';
var ARRAY_ATTR_REGEXPS = '_arrayAttrRegexps';
var DEPRECATED = '_deprecated';
var UNDERSCORE_ATTRS = [IS_SUBPLOT_OBJ, IS_LINKED_TO_ARRAY, ARRAY_ATTR_REGEXPS, DEPRECATED];
exports.IS_SUBPLOT_OBJ = IS_SUBPLOT_OBJ;
exports.IS_LINKED_TO_ARRAY = IS_LINKED_TO_ARRAY;
exports.DEPRECATED = DEPRECATED;
exports.UNDERSCORE_ATTRS = UNDERSCORE_ATTRS;
/** Outputs the full plotly.js plot schema
*
* @return {object}
* - defs
* - traces
* - layout
* - transforms
* - frames
* - animations
* - config (coming soon ...)
*/
exports.get = function() {
var traces = {};
Registry.allTypes.concat('area').forEach(function(type) {
traces[type] = getTraceAttributes(type);
});
var transforms = {};
Object.keys(Registry.transformsRegistry).forEach(function(type) {
transforms[type] = getTransformAttributes(type);
});
return {
defs: {
valObjects: Lib.valObjects,
metaKeys: UNDERSCORE_ATTRS.concat(['description', 'role'])
},
traces: traces,
layout: getLayoutAttributes(),
transforms: transforms,
frames: getFramesAttributes(),
animation: formatAttributes(animationAttributes)
};
};
/**
* Crawl the attribute tree, recursively calling a callback function
*
* @param {object} attrs
* The node of the attribute tree (e.g. the root) from which recursion originates
* @param {Function} callback
* A callback function with the signature:
* @callback callback
* @param {object} attr an attribute
* @param {String} attrName name string
* @param {object[]} attrs all the attributes
* @param {Number} level the recursion level, 0 at the root
* @param {Number} [specifiedLevel]
* The level in the tree, in order to let the callback function detect descend or backtrack,
* typically unsupplied (implied 0), just used by the self-recursive call.
* The necessity arises because the tree traversal is not controlled by callback return values.
* The decision to not use callback return values for controlling tree pruning arose from
* the goal of keeping the crawler backwards compatible. Observe that one of the pruning conditions
* precedes the callback call.
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.crawl = function(attrs, callback, specifiedLevel) {
var level = specifiedLevel || 0;
Object.keys(attrs).forEach(function(attrName) {
var attr = attrs[attrName];
if(UNDERSCORE_ATTRS.indexOf(attrName) !== -1) return;
callback(attr, attrName, attrs, level);
if(exports.isValObject(attr)) return;
if(Lib.isPlainObject(attr)) exports.crawl(attr, callback, level + 1);
});
};
/** Is object a value object (or a container object)?
*
* @param {object} obj
* @return {boolean}
* returns true for a valid value object and
* false for tree nodes in the attribute hierarchy
*/
exports.isValObject = function(obj) {
return obj && obj.valType !== undefined;
};
/**
* Find all data array attributes in a given trace object - including
* `arrayOk` attributes.
*
* @param {object} trace
* full trace object that contains a reference to `_module.attributes`
*
* @return {array} arrayAttributes
* list of array attributes for the given trace
*/
exports.findArrayAttributes = function(trace) {
var arrayAttributes = [],
stack = [];
function callback(attr, attrName, attrs, level) {
stack = stack.slice(0, level).concat([attrName]);
var splittableAttr = attr && (attr.valType === 'data_array' || attr.arrayOk === true);
if(!splittableAttr) return;
var astr = toAttrString(stack);
var val = Lib.nestedProperty(trace, astr).get();
if(!Array.isArray(val)) return;
arrayAttributes.push(astr);
}
function toAttrString(stack) {
return stack.join('.');
}
exports.crawl(trace._module.attributes, callback);
if(trace.transforms) {
var transforms = trace.transforms;
for(var i = 0; i < transforms.length; i++) {
var transform = transforms[i];
stack = ['transforms[' + i + ']'];
exports.crawl(transform._module.attributes, callback, 1);
}
}
// Look into the fullInput module attributes for array attributes
// to make sure that 'custom' array attributes are detected.
//
// At the moment, we need this block to make sure that
// ohlc and candlestick 'open', 'high', 'low', 'close' can be
// used with filter ang groupby transforms.
if(trace._fullInput) {
exports.crawl(trace._fullInput._module.attributes, callback);
arrayAttributes = Lib.filterUnique(arrayAttributes);
}
return arrayAttributes;
};
function getTraceAttributes(type) {
var _module, basePlotModule;
if(type === 'area') {
_module = { attributes: polarAreaAttrs };
basePlotModule = {};
}
else {
_module = Registry.modules[type]._module,
basePlotModule = _module.basePlotModule;
}
var attributes = {};
// make 'type' the first attribute in the object
attributes.type = null;
// base attributes (same for all trace types)
extendDeep(attributes, baseAttributes);
// module attributes
extendDeep(attributes, _module.attributes);
// subplot attributes
if(basePlotModule.attributes) {
extendDeep(attributes, basePlotModule.attributes);
}
// add registered components trace attributes
Object.keys(Registry.componentsRegistry).forEach(function(k) {
var _module = Registry.componentsRegistry[k];
if(_module.schema && _module.schema.traces && _module.schema.traces[type]) {
Object.keys(_module.schema.traces[type]).forEach(function(v) {
insertAttrs(attributes, _module.schema.traces[type][v], v);
});
}
});
// 'type' gets overwritten by baseAttributes; reset it here
attributes.type = type;
var out = {
meta: _module.meta || {},
attributes: formatAttributes(attributes),
};
// trace-specific layout attributes
if(_module.layoutAttributes) {
var layoutAttributes = {};
extendDeep(layoutAttributes, _module.layoutAttributes);
out.layoutAttributes = formatAttributes(layoutAttributes);
}
return out;
}
function getLayoutAttributes() {
var layoutAttributes = {};
// global layout attributes
extendDeep(layoutAttributes, baseLayoutAttributes);
// add base plot module layout attributes
Object.keys(Registry.subplotsRegistry).forEach(function(k) {
var _module = Registry.subplotsRegistry[k];
if(!_module.layoutAttributes) return;
if(_module.name === 'cartesian') {
handleBasePlotModule(layoutAttributes, _module, 'xaxis');
handleBasePlotModule(layoutAttributes, _module, 'yaxis');
}
else {
var astr = _module.attr === 'subplot' ? _module.name : _module.attr;
handleBasePlotModule(layoutAttributes, _module, astr);
}
});
// polar layout attributes
layoutAttributes = assignPolarLayoutAttrs(layoutAttributes);
// add registered components layout attributes
Object.keys(Registry.componentsRegistry).forEach(function(k) {
var _module = Registry.componentsRegistry[k];
if(!_module.layoutAttributes) return;
if(_module.schema && _module.schema.layout) {
Object.keys(_module.schema.layout).forEach(function(v) {
insertAttrs(layoutAttributes, _module.schema.layout[v], v);
});
}
else {
insertAttrs(layoutAttributes, _module.layoutAttributes, _module.name);
}
});
return {
layoutAttributes: formatAttributes(layoutAttributes)
};
}
function getTransformAttributes(type) {
var _module = Registry.transformsRegistry[type];
var attributes = extendDeep({}, _module.attributes);
// add registered components transform attributes
Object.keys(Registry.componentsRegistry).forEach(function(k) {
var _module = Registry.componentsRegistry[k];
if(_module.schema && _module.schema.transforms && _module.schema.transforms[type]) {
Object.keys(_module.schema.transforms[type]).forEach(function(v) {
insertAttrs(attributes, _module.schema.transforms[type][v], v);
});
}
});
return {
attributes: formatAttributes(attributes)
};
}
function getFramesAttributes() {
var attrs = {
frames: Lib.extendDeep({}, frameAttributes)
};
formatAttributes(attrs);
return attrs.frames;
}
function formatAttributes(attrs) {
mergeValTypeAndRole(attrs);
formatArrayContainers(attrs);
return attrs;
}
function mergeValTypeAndRole(attrs) {
function makeSrcAttr(attrName) {
return {
valType: 'string',
role: 'info',
description: [
'Sets the source reference on plot.ly for ',
attrName, '.'
].join(' ')
};
}
function callback(attr, attrName, attrs) {
if(exports.isValObject(attr)) {
if(attr.valType === 'data_array') {
// all 'data_array' attrs have role 'data'
attr.role = 'data';
// all 'data_array' attrs have a corresponding 'src' attr
attrs[attrName + 'src'] = makeSrcAttr(attrName);
}
else if(attr.arrayOk === true) {
// all 'arrayOk' attrs have a corresponding 'src' attr
attrs[attrName + 'src'] = makeSrcAttr(attrName);
}
}
else if(Lib.isPlainObject(attr)) {
// all attrs container objects get role 'object'
attr.role = 'object';
}
}
exports.crawl(attrs, callback);
}
function formatArrayContainers(attrs) {
function callback(attr, attrName, attrs) {
if(!attr) return;
var itemName = attr[IS_LINKED_TO_ARRAY];
if(!itemName) return;
delete attr[IS_LINKED_TO_ARRAY];
attrs[attrName] = { items: {} };
attrs[attrName].items[itemName] = attr;
attrs[attrName].role = 'object';
}
exports.crawl(attrs, callback);
}
function assignPolarLayoutAttrs(layoutAttributes) {
extendFlat(layoutAttributes, {
radialaxis: polarAxisAttrs.radialaxis,
angularaxis: polarAxisAttrs.angularaxis
});
extendFlat(layoutAttributes, polarAxisAttrs.layout);
return layoutAttributes;
}
function handleBasePlotModule(layoutAttributes, _module, astr) {
var np = Lib.nestedProperty(layoutAttributes, astr),
attrs = extendDeep({}, _module.layoutAttributes);
attrs[IS_SUBPLOT_OBJ] = true;
np.set(attrs);
}
function insertAttrs(baseAttrs, newAttrs, astr) {
var np = Lib.nestedProperty(baseAttrs, astr);
np.set(extendDeep(np.get() || {}, newAttrs));
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../registry');
var Lib = require('../lib');
module.exports = function register(_modules) {
if(!_modules) {
throw new Error('No argument passed to Plotly.register.');
}
else if(_modules && !Array.isArray(_modules)) {
_modules = [_modules];
}
for(var i = 0; i < _modules.length; i++) {
var newModule = _modules[i];
if(!newModule) {
throw new Error('Invalid module was attempted to be registered!');
}
switch(newModule.moduleType) {
case 'trace':
registerTraceModule(newModule);
break;
case 'transform':
registerTransformModule(newModule);
break;
case 'component':
registerComponentModule(newModule);
break;
default:
throw new Error('Invalid module was attempted to be registered!');
}
}
};
function registerTraceModule(newModule) {
Registry.register(newModule, newModule.name, newModule.categories, newModule.meta);
if(!Registry.subplotsRegistry[newModule.basePlotModule.name]) {
Registry.registerSubplot(newModule.basePlotModule);
}
}
function registerTransformModule(newModule) {
if(typeof newModule.name !== 'string') {
throw new Error('Transform module *name* must be a string.');
}
var prefix = 'Transform module ' + newModule.name;
var hasTransform = typeof newModule.transform === 'function',
hasCalcTransform = typeof newModule.calcTransform === 'function';
if(!hasTransform && !hasCalcTransform) {
throw new Error(prefix + ' is missing a *transform* or *calcTransform* method.');
}
if(hasTransform && hasCalcTransform) {
Lib.log([
prefix + ' has both a *transform* and *calcTransform* methods.',
'Please note that all *transform* methods are executed',
'before all *calcTransform* methods.'
].join(' '));
}
if(!Lib.isPlainObject(newModule.attributes)) {
Lib.log(prefix + ' registered without an *attributes* object.');
}
if(typeof newModule.supplyDefaults !== 'function') {
Lib.log(prefix + ' registered without a *supplyDefaults* method.');
}
Registry.transformsRegistry[newModule.name] = newModule;
}
function registerComponentModule(newModule) {
if(typeof newModule.name !== 'string') {
throw new Error('Component module *name* must be a string.');
}
Registry.registerComponent(newModule);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../plotly');
var Lib = require('../lib');
/**
* Extends the plot config
*
* @param {object} configObj partial plot configuration object
* to extend the current plot configuration.
*
*/
module.exports = function setPlotConfig(configObj) {
return Lib.extendFlat(Plotly.defaultConfig, configObj);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Plotly = require('../plotly');
var Registry = require('../registry');
var Plots = require('../plots/plots');
var Lib = require('../lib');
var Color = require('../components/color');
var Drawing = require('../components/drawing');
var Titles = require('../components/titles');
var ModeBar = require('../components/modebar');
exports.layoutStyles = function(gd) {
return Lib.syncOrAsync([Plots.doAutoMargin, exports.lsInner], gd);
};
function overlappingDomain(xDomain, yDomain, domains) {
for(var i = 0; i < domains.length; i++) {
var existingX = domains[i][0],
existingY = domains[i][1];
if(existingX[0] >= xDomain[1] || existingX[1] <= xDomain[0]) {
continue;
}
if(existingY[0] < yDomain[1] && existingY[1] > yDomain[0]) {
return true;
}
}
return false;
}
exports.lsInner = function(gd) {
var fullLayout = gd._fullLayout,
gs = fullLayout._size,
axList = Plotly.Axes.list(gd),
i;
// clear axis line positions, to be set in the subplot loop below
for(i = 0; i < axList.length; i++) axList[i]._linepositions = {};
fullLayout._paperdiv
.style({
width: fullLayout.width + 'px',
height: fullLayout.height + 'px'
})
.selectAll('.main-svg')
.call(Drawing.setSize, fullLayout.width, fullLayout.height);
gd._context.setBackground(gd, fullLayout.paper_bgcolor);
var subplotSelection = fullLayout._paper.selectAll('g.subplot');
// figure out which backgrounds we need to draw, and in which layers
// to put them
var lowerBackgroundIDs = [];
var lowerDomains = [];
subplotSelection.each(function(subplot) {
var plotinfo = fullLayout._plots[subplot];
if(plotinfo.mainplot) {
// mainplot is a reference to the main plot this one is overlaid on
// so if it exists, this is an overlaid plot and we don't need to
// give it its own background
if(plotinfo.bg) {
plotinfo.bg.remove();
}
plotinfo.bg = undefined;
return;
}
var xa = Plotly.Axes.getFromId(gd, subplot, 'x'),
ya = Plotly.Axes.getFromId(gd, subplot, 'y'),
xDomain = xa.domain,
yDomain = ya.domain,
plotgroupBgData = [];
if(overlappingDomain(xDomain, yDomain, lowerDomains)) {
plotgroupBgData = [0];
}
else {
lowerBackgroundIDs.push(subplot);
lowerDomains.push([xDomain, yDomain]);
}
// create the plot group backgrounds now, since
// they're all independent selections
var plotgroupBg = plotinfo.plotgroup.selectAll('.bg')
.data(plotgroupBgData);
plotgroupBg.enter().append('rect')
.classed('bg', true);
plotgroupBg.exit().remove();
plotgroupBg.each(function() {
plotinfo.bg = plotgroupBg;
var pgNode = plotinfo.plotgroup.node();
pgNode.insertBefore(this, pgNode.childNodes[0]);
});
});
// now create all the lower-layer backgrounds at once now that
// we have the list of subplots that need them
var lowerBackgrounds = fullLayout._bgLayer.selectAll('.bg')
.data(lowerBackgroundIDs);
lowerBackgrounds.enter().append('rect')
.classed('bg', true);
lowerBackgrounds.exit().remove();
lowerBackgrounds.each(function(subplot) {
fullLayout._plots[subplot].bg = d3.select(this);
});
var freefinished = [];
subplotSelection.each(function(subplot) {
var plotinfo = fullLayout._plots[subplot],
xa = Plotly.Axes.getFromId(gd, subplot, 'x'),
ya = Plotly.Axes.getFromId(gd, subplot, 'y');
// reset scale in case the margins have changed
xa.setScale();
ya.setScale();
if(plotinfo.bg) {
plotinfo.bg
.call(Drawing.setRect,
xa._offset - gs.p, ya._offset - gs.p,
xa._length + 2 * gs.p, ya._length + 2 * gs.p)
.call(Color.fill, fullLayout.plot_bgcolor)
.style('stroke-width', 0);
}
// Clip so that data only shows up on the plot area.
plotinfo.clipId = 'clip' + fullLayout._uid + subplot + 'plot';
var plotClip = fullLayout._defs.selectAll('g.clips')
.selectAll('#' + plotinfo.clipId)
.data([0]);
plotClip.enter().append('clipPath')
.attr({
'class': 'plotclip',
'id': plotinfo.clipId
})
.append('rect');
plotClip.selectAll('rect')
.attr({
'width': xa._length,
'height': ya._length
});
plotinfo.plot.call(Drawing.setTranslate, xa._offset, ya._offset);
plotinfo.plot.call(Drawing.setClipUrl, plotinfo.clipId);
var xlw = Drawing.crispRound(gd, xa.linewidth, 1),
ylw = Drawing.crispRound(gd, ya.linewidth, 1),
xp = gs.p + ylw,
xpathPrefix = 'M' + (-xp) + ',',
xpathSuffix = 'h' + (xa._length + 2 * xp),
showfreex = xa.anchor === 'free' &&
freefinished.indexOf(xa._id) === -1,
freeposx = gs.h * (1 - (xa.position||0)) + ((xlw / 2) % 1),
showbottom =
(xa.anchor === ya._id && (xa.mirror || xa.side !== 'top')) ||
xa.mirror === 'all' || xa.mirror === 'allticks' ||
(xa.mirrors && xa.mirrors[ya._id + 'bottom']),
bottompos = ya._length + gs.p + xlw / 2,
showtop =
(xa.anchor === ya._id && (xa.mirror || xa.side === 'top')) ||
xa.mirror === 'all' || xa.mirror === 'allticks' ||
(xa.mirrors && xa.mirrors[ya._id + 'top']),
toppos = -gs.p - xlw / 2,
// shorten y axis lines so they don't overlap x axis lines
yp = gs.p,
// except where there's no x line
// TODO: this gets more complicated with multiple x and y axes
ypbottom = showbottom ? 0 : xlw,
yptop = showtop ? 0 : xlw,
ypathSuffix = ',' + (-yp - yptop) +
'v' + (ya._length + 2 * yp + yptop + ypbottom),
showfreey = ya.anchor === 'free' &&
freefinished.indexOf(ya._id) === -1,
freeposy = gs.w * (ya.position||0) + ((ylw / 2) % 1),
showleft =
(ya.anchor === xa._id && (ya.mirror || ya.side !== 'right')) ||
ya.mirror === 'all' || ya.mirror === 'allticks' ||
(ya.mirrors && ya.mirrors[xa._id + 'left']),
leftpos = -gs.p - ylw / 2,
showright =
(ya.anchor === xa._id && (ya.mirror || ya.side === 'right')) ||
ya.mirror === 'all' || ya.mirror === 'allticks' ||
(ya.mirrors && ya.mirrors[xa._id + 'right']),
rightpos = xa._length + gs.p + ylw / 2;
// save axis line positions for ticks, draggers, etc to reference
// each subplot gets an entry:
// [left or bottom, right or top, free, main]
// main is the position at which to draw labels and draggers, if any
xa._linepositions[subplot] = [
showbottom ? bottompos : undefined,
showtop ? toppos : undefined,
showfreex ? freeposx : undefined
];
if(xa.anchor === ya._id) {
xa._linepositions[subplot][3] = xa.side === 'top' ?
toppos : bottompos;
}
else if(showfreex) {
xa._linepositions[subplot][3] = freeposx;
}
ya._linepositions[subplot] = [
showleft ? leftpos : undefined,
showright ? rightpos : undefined,
showfreey ? freeposy : undefined
];
if(ya.anchor === xa._id) {
ya._linepositions[subplot][3] = ya.side === 'right' ?
rightpos : leftpos;
}
else if(showfreey) {
ya._linepositions[subplot][3] = freeposy;
}
// translate all the extra stuff to have the
// same origin as the plot area or axes
var origin = 'translate(' + xa._offset + ',' + ya._offset + ')',
originx = origin,
originy = origin;
if(showfreex) {
originx = 'translate(' + xa._offset + ',' + gs.t + ')';
toppos += ya._offset - gs.t;
bottompos += ya._offset - gs.t;
}
if(showfreey) {
originy = 'translate(' + gs.l + ',' + ya._offset + ')';
leftpos += xa._offset - gs.l;
rightpos += xa._offset - gs.l;
}
plotinfo.xlines
.attr('transform', originx)
.attr('d', (
(showbottom ? (xpathPrefix + bottompos + xpathSuffix) : '') +
(showtop ? (xpathPrefix + toppos + xpathSuffix) : '') +
(showfreex ? (xpathPrefix + freeposx + xpathSuffix) : '')) ||
// so it doesn't barf with no lines shown
'M0,0')
.style('stroke-width', xlw + 'px')
.call(Color.stroke, xa.showline ?
xa.linecolor : 'rgba(0,0,0,0)');
plotinfo.ylines
.attr('transform', originy)
.attr('d', (
(showleft ? ('M' + leftpos + ypathSuffix) : '') +
(showright ? ('M' + rightpos + ypathSuffix) : '') +
(showfreey ? ('M' + freeposy + ypathSuffix) : '')) ||
'M0,0')
.attr('stroke-width', ylw + 'px')
.call(Color.stroke, ya.showline ?
ya.linecolor : 'rgba(0,0,0,0)');
plotinfo.xaxislayer.attr('transform', originx);
plotinfo.yaxislayer.attr('transform', originy);
plotinfo.gridlayer.attr('transform', origin);
plotinfo.zerolinelayer.attr('transform', origin);
plotinfo.draglayer.attr('transform', origin);
// mark free axes as displayed, so we don't draw them again
if(showfreex) { freefinished.push(xa._id); }
if(showfreey) { freefinished.push(ya._id); }
});
Plotly.Axes.makeClipPaths(gd);
exports.drawMainTitle(gd);
ModeBar.manage(gd);
return gd._promises.length && Promise.all(gd._promises);
};
exports.drawMainTitle = function(gd) {
var fullLayout = gd._fullLayout;
Titles.draw(gd, 'gtitle', {
propContainer: fullLayout,
propName: 'title',
dfltName: 'Plot',
attributes: {
x: fullLayout.width / 2,
y: fullLayout._size.t / 2,
'text-anchor': 'middle'
}
});
};
// First, see if we need to do arraysToCalcdata
// call it regardless of what change we made, in case
// supplyDefaults brought in an array that was already
// in gd.data but not in gd._fullData previously
exports.doTraceStyle = function(gd) {
for(var i = 0; i < gd.calcdata.length; i++) {
var cdi = gd.calcdata[i],
_module = ((cdi[0] || {}).trace || {})._module || {},
arraysToCalcdata = _module.arraysToCalcdata;
if(arraysToCalcdata) arraysToCalcdata(cdi, cdi[0].trace);
}
Plots.style(gd);
Registry.getComponentMethod('legend', 'draw')(gd);
return Plots.previousPromises(gd);
};
exports.doColorBars = function(gd) {
for(var i = 0; i < gd.calcdata.length; i++) {
var cdi0 = gd.calcdata[i][0];
if((cdi0.t || {}).cb) {
var trace = cdi0.trace,
cb = cdi0.t.cb;
if(Registry.traceIs(trace, 'contour')) {
cb.line({
width: trace.contours.showlines !== false ?
trace.line.width : 0,
dash: trace.line.dash,
color: trace.contours.coloring === 'line' ?
cb._opts.line.color : trace.line.color
});
}
if(Registry.traceIs(trace, 'markerColorscale')) {
cb.options(trace.marker.colorbar)();
}
else cb.options(trace.colorbar)();
}
}
return Plots.previousPromises(gd);
};
// force plot() to redo the layout and replot with the modified layout
exports.layoutReplot = function(gd) {
var layout = gd.layout;
gd.layout = undefined;
return Plotly.plot(gd, '', layout);
};
exports.doLegend = function(gd) {
Registry.getComponentMethod('legend', 'draw')(gd);
return Plots.previousPromises(gd);
};
exports.doTicksRelayout = function(gd) {
Plotly.Axes.doTicks(gd, 'redraw');
exports.drawMainTitle(gd);
return Plots.previousPromises(gd);
};
exports.doModeBar = function(gd) {
var fullLayout = gd._fullLayout;
var subplotIds, i;
ModeBar.manage(gd);
Plotly.Fx.init(gd);
subplotIds = Plots.getSubplotIds(fullLayout, 'gl3d');
for(i = 0; i < subplotIds.length; i++) {
var scene = fullLayout[subplotIds[i]]._scene;
scene.updateFx(fullLayout.dragmode, fullLayout.hovermode);
}
// no need to do this for gl2d subplots,
// Plots.linkSubplots takes care of it all.
return Plots.previousPromises(gd);
};
exports.doCamera = function(gd) {
var fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');
for(var i = 0; i < sceneIds.length; i++) {
var sceneLayout = fullLayout[sceneIds[i]],
scene = sceneLayout._scene;
scene.setCamera(sceneLayout.camera);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Plotly = require('../plotly');
var Lib = require('../lib');
var helpers = require('../snapshot/helpers');
var clonePlot = require('../snapshot/cloneplot');
var toSVG = require('../snapshot/tosvg');
var svgToImg = require('../snapshot/svgtoimg');
/**
* @param {object} gd figure Object
* @param {object} opts option object
* @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
* @param opts.width width of snapshot in px
* @param opts.height height of snapshot in px
*/
function toImage(gd, opts) {
var promise = new Promise(function(resolve, reject) {
// check for undefined opts
opts = opts || {};
// default to png
opts.format = opts.format || 'png';
var isSizeGood = function(size) {
// undefined and null are valid options
if(size === undefined || size === null) {
return true;
}
if(isNumeric(size) && size > 1) {
return true;
}
return false;
};
if(!isSizeGood(opts.width) || !isSizeGood(opts.height)) {
reject(new Error('Height and width should be pixel values.'));
}
// first clone the GD so we can operate in a clean environment
var clone = clonePlot(gd, {format: 'png', height: opts.height, width: opts.width});
var clonedGd = clone.gd;
// put the cloned div somewhere off screen before attaching to DOM
clonedGd.style.position = 'absolute';
clonedGd.style.left = '-5000px';
document.body.appendChild(clonedGd);
function wait() {
var delay = helpers.getDelay(clonedGd._fullLayout);
return new Promise(function(resolve, reject) {
setTimeout(function() {
var svg = toSVG(clonedGd);
var canvas = document.createElement('canvas');
canvas.id = Lib.randstr();
svgToImg({
format: opts.format,
width: clonedGd._fullLayout.width,
height: clonedGd._fullLayout.height,
canvas: canvas,
svg: svg,
// ask svgToImg to return a Promise
// rather than EventEmitter
// leave EventEmitter for backward
// compatibility
promise: true
}).then(function(url) {
if(clonedGd) document.body.removeChild(clonedGd);
resolve(url);
}).catch(function(err) {
reject(err);
});
}, delay);
});
}
var redrawFunc = helpers.getRedrawFunc(clonedGd);
Plotly.plot(clonedGd, clone.data, clone.layout, clone.config)
.then(redrawFunc)
.then(wait)
.then(function(url) { resolve(url); })
.catch(function(err) {
reject(err);
});
});
return promise;
}
module.exports = toImage;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | 2 2 2 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var Plots = require('../plots/plots');
var PlotSchema = require('./plot_schema');
var isPlainObject = Lib.isPlainObject;
var isArray = Array.isArray;
/**
* Validate a data array and layout object.
*
* @param {array} data
* @param {object} layout
*
* @return {array} array of error objects each containing:
* - {string} code
* error code ('object', 'array', 'schema', 'unused', 'invisible' or 'value')
* - {string} container
* container where the error occurs ('data' or 'layout')
* - {number} trace
* trace index of the 'data' container where the error occurs
* - {array} path
* nested path to the key that causes the error
* - {string} astr
* attribute string variant of 'path' compatible with Plotly.restyle and
* Plotly.relayout.
* - {string} msg
* error message (shown in console in logger config argument is enable)
*/
module.exports = function valiate(data, layout) {
var schema = PlotSchema.get(),
errorList = [],
gd = {};
var dataIn, layoutIn;
if(isArray(data)) {
gd.data = Lib.extendDeep([], data);
dataIn = data;
}
else {
gd.data = [];
dataIn = [];
errorList.push(format('array', 'data'));
}
if(isPlainObject(layout)) {
gd.layout = Lib.extendDeep({}, layout);
layoutIn = layout;
}
else {
gd.layout = {};
layoutIn = {};
if(arguments.length > 1) {
errorList.push(format('object', 'layout'));
}
}
// N.B. dataIn and layoutIn are in general not the same as
// gd.data and gd.layout after supplyDefaults as some attributes
// in gd.data and gd.layout (still) get mutated during this step.
Plots.supplyDefaults(gd);
var dataOut = gd._fullData,
len = dataIn.length;
for(var i = 0; i < len; i++) {
var traceIn = dataIn[i],
base = ['data', i];
if(!isPlainObject(traceIn)) {
errorList.push(format('object', base));
continue;
}
var traceOut = dataOut[i],
traceType = traceOut.type,
traceSchema = schema.traces[traceType].attributes;
// PlotSchema does something fancy with trace 'type', reset it here
// to make the trace schema compatible with Lib.validate.
traceSchema.type = {
valType: 'enumerated',
values: [traceType]
};
if(traceOut.visible === false && traceIn.visible !== false) {
errorList.push(format('invisible', base));
}
crawl(traceIn, traceOut, traceSchema, errorList, base);
var transformsIn = traceIn.transforms,
transformsOut = traceOut.transforms;
if(transformsIn) {
if(!isArray(transformsIn)) {
errorList.push(format('array', base, ['transforms']));
}
base.push('transforms');
for(var j = 0; j < transformsIn.length; j++) {
var path = ['transforms', j],
transformType = transformsIn[j].type;
if(!isPlainObject(transformsIn[j])) {
errorList.push(format('object', base, path));
continue;
}
var transformSchema = schema.transforms[transformType] ?
schema.transforms[transformType].attributes :
{};
// add 'type' to transform schema to validate the transform type
transformSchema.type = {
valType: 'enumerated',
values: Object.keys(schema.transforms)
};
crawl(transformsIn[j], transformsOut[j], transformSchema, errorList, base, path);
}
}
}
var layoutOut = gd._fullLayout,
layoutSchema = fillLayoutSchema(schema, dataOut);
crawl(layoutIn, layoutOut, layoutSchema, errorList, 'layout');
// return undefined if no validation errors were found
return (errorList.length === 0) ? void(0) : errorList;
};
function crawl(objIn, objOut, schema, list, base, path) {
path = path || [];
var keys = Object.keys(objIn);
for(var i = 0; i < keys.length; i++) {
var k = keys[i];
// transforms are handled separately
if(k === 'transforms') continue;
var p = path.slice();
p.push(k);
var valIn = objIn[k],
valOut = objOut[k];
var nestedSchema = getNestedSchema(schema, k),
isInfoArray = (nestedSchema || {}).valType === 'info_array',
isColorscale = (nestedSchema || {}).valType === 'colorscale';
if(!isInSchema(schema, k)) {
list.push(format('schema', base, p));
}
else if(isPlainObject(valIn) && isPlainObject(valOut)) {
crawl(valIn, valOut, nestedSchema, list, base, p);
}
else if(nestedSchema.items && !isInfoArray && isArray(valIn)) {
var items = nestedSchema.items,
_nestedSchema = items[Object.keys(items)[0]],
indexList = [];
var j, _p;
// loop over valOut items while keeping track of their
// corresponding input container index (given by _index)
for(j = 0; j < valOut.length; j++) {
var _index = valOut[j]._index || j;
_p = p.slice();
_p.push(_index);
if(isPlainObject(valIn[_index]) && isPlainObject(valOut[j])) {
indexList.push(_index);
crawl(valIn[_index], valOut[j], _nestedSchema, list, base, _p);
}
}
// loop over valIn to determine where it went wrong for some items
for(j = 0; j < valIn.length; j++) {
_p = p.slice();
_p.push(j);
if(!isPlainObject(valIn[j])) {
list.push(format('object', base, _p, valIn[j]));
}
else if(indexList.indexOf(j) === -1) {
list.push(format('unused', base, _p));
}
}
}
else if(!isPlainObject(valIn) && isPlainObject(valOut)) {
list.push(format('object', base, p, valIn));
}
else if(!isArray(valIn) && isArray(valOut) && !isInfoArray && !isColorscale) {
list.push(format('array', base, p, valIn));
}
else if(!(k in objOut)) {
list.push(format('unused', base, p, valIn));
}
else if(!Lib.validate(valIn, nestedSchema)) {
list.push(format('value', base, p, valIn));
}
}
return list;
}
// the 'full' layout schema depends on the traces types presents
function fillLayoutSchema(schema, dataOut) {
for(var i = 0; i < dataOut.length; i++) {
var traceType = dataOut[i].type,
traceLayoutAttr = schema.traces[traceType].layoutAttributes;
if(traceLayoutAttr) {
Lib.extendFlat(schema.layout.layoutAttributes, traceLayoutAttr);
}
}
return schema.layout.layoutAttributes;
}
// validation error codes
var code2msgFunc = {
object: function(base, astr) {
var prefix;
if(base === 'layout' && astr === '') prefix = 'The layout argument';
else if(base[0] === 'data' && astr === '') {
prefix = 'Trace ' + base[1] + ' in the data argument';
}
else prefix = inBase(base) + 'key ' + astr;
return prefix + ' must be linked to an object container';
},
array: function(base, astr) {
var prefix;
if(base === 'data') prefix = 'The data argument';
else prefix = inBase(base) + 'key ' + astr;
return prefix + ' must be linked to an array container';
},
schema: function(base, astr) {
return inBase(base) + 'key ' + astr + ' is not part of the schema';
},
unused: function(base, astr, valIn) {
var target = isPlainObject(valIn) ? 'container' : 'key';
return inBase(base) + target + ' ' + astr + ' did not get coerced';
},
invisible: function(base) {
return 'Trace ' + base[1] + ' got defaulted to be not visible';
},
value: function(base, astr, valIn) {
return [
inBase(base) + 'key ' + astr,
'is set to an invalid value (' + valIn + ')'
].join(' ');
}
};
function inBase(base) {
if(isArray(base)) return 'In data trace ' + base[1] + ', ';
return 'In ' + base + ', ';
}
function format(code, base, path, valIn) {
path = path || '';
var container, trace;
// container is either 'data' or 'layout
// trace is the trace index if 'data', null otherwise
if(isArray(base)) {
container = base[0];
trace = base[1];
}
else {
container = base;
trace = null;
}
var astr = convertPathToAttributeString(path),
msg = code2msgFunc[code](base, astr, valIn);
// log to console if logger config option is enabled
Lib.log(msg);
return {
code: code,
container: container,
trace: trace,
path: path,
astr: astr,
msg: msg
};
}
function isInSchema(schema, key) {
var parts = splitKey(key),
keyMinusId = parts.keyMinusId,
id = parts.id;
if((keyMinusId in schema) && schema[keyMinusId]._isSubplotObj && id) {
return true;
}
return (key in schema);
}
function getNestedSchema(schema, key) {
var parts = splitKey(key);
return schema[parts.keyMinusId];
}
function splitKey(key) {
var idRegex = /([2-9]|[1-9][0-9]+)$/;
var keyMinusId = key.split(idRegex)[0],
id = key.substr(keyMinusId.length, key.length);
return {
keyMinusId: keyMinusId,
id: id
};
}
function convertPathToAttributeString(path) {
if(!isArray(path)) return String(path);
var astr = '';
for(var i = 0; i < path.length; i++) {
var p = path[i];
if(typeof p === 'number') {
astr = astr.substr(0, astr.length - 1) + '[' + p + ']';
}
else {
astr += p;
}
if(i < path.length - 1) astr += '.';
}
return astr;
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| animation_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| array_container_defaults.js | 11.11% | (2 / 18) | 0% | (0 / 6) | 0% | (0 / 1) | 11.11% | (2 / 18) | |
| attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| command.js | 5.76% | (11 / 191) | 0% | (0 / 114) | 0% | (0 / 18) | 5.91% | (11 / 186) | |
| font_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| frame_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| layout_attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| pad_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| plots.js | 8.98% | (87 / 969) | 0% | (0 / 592) | 0% | (0 / 79) | 9.7% | (87 / 897) | |
| subplot_defaults.js | 22.22% | (4 / 18) | 0% | (0 / 4) | 0% | (0 / 2) | 23.53% | (4 / 17) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
mode: {
valType: 'enumerated',
dflt: 'afterall',
role: 'info',
values: ['immediate', 'next', 'afterall'],
description: [
'Describes how a new animate call interacts with currently-running',
'animations. If `immediate`, current animations are interrupted and',
'the new animation is started. If `next`, the current frame is allowed',
'to complete, after which the new animation is started. If `afterall`',
'all existing frames are animated to completion before the new animation',
'is started.'
].join(' ')
},
direction: {
valType: 'enumerated',
role: 'info',
values: ['forward', 'reverse'],
dflt: 'forward',
description: [
'The direction in which to play the frames triggered by the animation call'
].join(' ')
},
fromcurrent: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'Play frames starting at the current frame instead of the beginning.'
].join(' ')
},
frame: {
duration: {
valType: 'number',
role: 'info',
min: 0,
dflt: 500,
description: [
'The duration in milliseconds of each frame. If greater than the frame',
'duration, it will be limited to the frame duration.'
].join(' ')
},
redraw: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Redraw the plot at completion of the transition. This is desirable',
'for transitions that include properties that cannot be transitioned,',
'but may significantly slow down updates that do not require a full',
'redraw of the plot'
].join(' ')
},
},
transition: {
duration: {
valType: 'number',
role: 'info',
min: 0,
dflt: 500,
description: [
'The duration of the transition, in milliseconds. If equal to zero,',
'updates are synchronous.'
].join(' ')
},
easing: {
valType: 'enumerated',
dflt: 'cubic-in-out',
values: [
'linear',
'quad',
'cubic',
'sin',
'exp',
'circle',
'elastic',
'back',
'bounce',
'linear-in',
'quad-in',
'cubic-in',
'sin-in',
'exp-in',
'circle-in',
'elastic-in',
'back-in',
'bounce-in',
'linear-out',
'quad-out',
'cubic-out',
'sin-out',
'exp-out',
'circle-out',
'elastic-out',
'back-out',
'bounce-out',
'linear-in-out',
'quad-in-out',
'cubic-in-out',
'sin-in-out',
'exp-in-out',
'circle-in-out',
'elastic-in-out',
'back-in-out',
'bounce-in-out'
],
role: 'info',
description: 'The easing function used for the transition'
},
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
/** Convenience wrapper for making array container logic DRY and consistent
*
* @param {object} parentObjIn
* user input object where the container in question is linked
* (i.e. either a user trace object or the user layout object)
*
* @param {object} parentObjOut
* full object where the coerced container will be linked
* (i.e. either a full trace object or the full layout object)
*
* @param {object} opts
* options object:
* - name {string}
* name of the key linking the container in question
* - handleItemDefaults {function}
* defaults method to be called on each item in the array container in question
*
* Its arguments are:
* - itemIn {object} item in user layout
* - itemOut {object} item in full layout
* - parentObj {object} (as in closure)
* - opts {object} (as in closure)
* - itemOpts {object}
* - itemIsNotPlainObject {boolean}
* N.B.
*
* - opts is passed to handleItemDefaults so it can also store
* links to supplementary data (e.g. fullData for layout components)
*
*/
module.exports = function handleArrayContainerDefaults(parentObjIn, parentObjOut, opts) {
var name = opts.name;
var previousContOut = parentObjOut[name];
var contIn = Lib.isArray(parentObjIn[name]) ? parentObjIn[name] : [],
contOut = parentObjOut[name] = [],
i;
for(i = 0; i < contIn.length; i++) {
var itemIn = contIn[i],
itemOut = {},
itemOpts = {};
if(!Lib.isPlainObject(itemIn)) {
itemOpts.itemIsNotPlainObject = true;
itemIn = {};
}
opts.handleItemDefaults(itemIn, itemOut, parentObjOut, opts, itemOpts);
itemOut._input = itemIn;
itemOut._index = i;
contOut.push(itemOut);
}
// in case this array gets its defaults rebuilt independent of the whole layout,
// relink the private keys just for this array.
if(Lib.isArray(previousContOut)) {
var len = Math.min(previousContOut.length, contOut.length);
for(i = 0; i < len; i++) {
Lib.relinkPrivateKeys(contOut[i], previousContOut[i]);
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
type: {
valType: 'enumerated',
role: 'info',
values: [], // listed dynamically
dflt: 'scatter'
},
visible: {
valType: 'enumerated',
values: [true, false, 'legendonly'],
role: 'info',
dflt: true,
description: [
'Determines whether or not this trace is visible.',
'If *legendonly*, the trace is not drawn,',
'but can appear as a legend item',
'(provided that the legend itself is visible).'
].join(' ')
},
showlegend: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not an item corresponding to this',
'trace is shown in the legend.'
].join(' ')
},
legendgroup: {
valType: 'string',
role: 'info',
dflt: '',
description: [
'Sets the legend group for this trace.',
'Traces part of the same legend group hide/show at the same time',
'when toggling legend items.'
].join(' ')
},
opacity: {
valType: 'number',
role: 'style',
min: 0,
max: 1,
dflt: 1,
description: 'Sets the opacity of the trace.'
},
name: {
valType: 'string',
role: 'info',
description: [
'Sets the trace name.',
'The trace name appear as the legend item and on hover.'
].join(' ')
},
uid: {
valType: 'string',
role: 'info',
dflt: ''
},
hoverinfo: {
valType: 'flaglist',
role: 'info',
flags: ['x', 'y', 'z', 'text', 'name'],
extras: ['all', 'none', 'skip'],
dflt: 'all',
description: [
'Determines which trace information appear on hover.',
'If `none` or `skip` are set, no information is displayed upon hovering.',
'But, if `none` is set, click and hover events are still fired.'
].join(' ')
},
stream: {
token: {
valType: 'string',
noBlank: true,
strict: true,
role: 'info',
description: [
'The stream id number links a data trace on a plot with a stream.',
'See https://plot.ly/settings for more details.'
].join(' ')
},
maxpoints: {
valType: 'number',
min: 0,
max: 10000,
dflt: 500,
role: 'info',
description: [
'Sets the maximum number of points to keep on the plots from an',
'incoming stream.',
'If `maxpoints` is set to *50*, only the newest 50 points will',
'be displayed on the plot.'
].join(' ')
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plotly = require('../plotly');
var Lib = require('../lib');
/*
* Create or update an observer. This function is designed to be
* idempotent so that it can be called over and over as the component
* updates, and will attach and detach listeners as needed.
*
* @param {optional object} container
* An object on which the observer is stored. This is the mechanism
* by which it is idempotent. If it already exists, another won't be
* added. Each time it's called, the value lookup table is updated.
* @param {array} commandList
* An array of commands, following either `buttons` of `updatemenus`
* or `steps` of `sliders`.
* @param {function} onchange
* A listener called when the value is changed. Receives data object
* with information about the new state.
*/
exports.manageCommandObserver = function(gd, container, commandList, onchange) {
var ret = {};
var enabled = true;
if(container && container._commandObserver) {
ret = container._commandObserver;
}
if(!ret.cache) {
ret.cache = {};
}
// Either create or just recompute this:
ret.lookupTable = {};
var binding = exports.hasSimpleAPICommandBindings(gd, commandList, ret.lookupTable);
if(container && container._commandObserver) {
if(!binding) {
// If container exists and there are no longer any bindings,
// remove existing:
if(container._commandObserver.remove) {
container._commandObserver.remove();
container._commandObserver = null;
return ret;
}
} else {
// If container exists and there *are* bindings, then the lookup
// table should have been updated and check is already attached,
// so there's nothing to be done:
return ret;
}
}
// Determine whether there's anything to do for this binding:
if(binding) {
// Build the cache:
bindingValueHasChanged(gd, binding, ret.cache);
ret.check = function check() {
if(!enabled) return;
var update = bindingValueHasChanged(gd, binding, ret.cache);
if(update.changed && onchange) {
// Disable checks for the duration of this command in order to avoid
// infinite loops:
if(ret.lookupTable[update.value] !== undefined) {
ret.disable();
Promise.resolve(onchange({
value: update.value,
type: binding.type,
prop: binding.prop,
traces: binding.traces,
index: ret.lookupTable[update.value]
})).then(ret.enable, ret.enable);
}
}
return update.changed;
};
var checkEvents = [
'plotly_relayout',
'plotly_redraw',
'plotly_restyle',
'plotly_update',
'plotly_animatingframe',
'plotly_afterplot'
];
for(var i = 0; i < checkEvents.length; i++) {
gd._internalOn(checkEvents[i], ret.check);
}
ret.remove = function() {
for(var i = 0; i < checkEvents.length; i++) {
gd._removeInternalListener(checkEvents[i], ret.check);
}
};
} else {
// TODO: It'd be really neat to actually give a *reason* for this, but at least a warning
// is a start
Lib.warn('Unable to automatically bind plot updates to API command');
ret.lookupTable = {};
ret.remove = function() {};
}
ret.disable = function disable() {
enabled = false;
};
ret.enable = function enable() {
enabled = true;
};
if(container) {
container._commandObserver = ret;
}
return ret;
};
/*
* This function checks to see if an array of objects containing
* method and args properties is compatible with automatic two-way
* binding. The criteria right now are that
*
* 1. multiple traces may be affected
* 2. only one property may be affected
* 3. the same property must be affected by all commands
*/
exports.hasSimpleAPICommandBindings = function(gd, commandList, bindingsByValue) {
var i;
var n = commandList.length;
var refBinding;
for(i = 0; i < n; i++) {
var binding;
var command = commandList[i];
var method = command.method;
var args = command.args;
if(!Array.isArray(args)) args = [];
// If any command has no method, refuse to bind:
if(!method) {
return false;
}
var bindings = exports.computeAPICommandBindings(gd, method, args);
// Right now, handle one and *only* one property being set:
if(bindings.length !== 1) {
return false;
}
if(!refBinding) {
refBinding = bindings[0];
if(Array.isArray(refBinding.traces)) {
refBinding.traces.sort();
}
} else {
binding = bindings[0];
if(binding.type !== refBinding.type) {
return false;
}
if(binding.prop !== refBinding.prop) {
return false;
}
if(Array.isArray(refBinding.traces)) {
if(Array.isArray(binding.traces)) {
binding.traces.sort();
for(var j = 0; j < refBinding.traces.length; j++) {
if(refBinding.traces[j] !== binding.traces[j]) {
return false;
}
}
} else {
return false;
}
} else {
if(binding.prop !== refBinding.prop) {
return false;
}
}
}
binding = bindings[0];
var value = binding.value;
if(Array.isArray(value)) {
if(value.length === 1) {
value = value[0];
} else {
return false;
}
}
if(bindingsByValue) {
bindingsByValue[value] = i;
}
}
return refBinding;
};
function bindingValueHasChanged(gd, binding, cache) {
var container, value, obj;
var changed = false;
if(binding.type === 'data') {
// If it's data, we need to get a trace. Based on the limited scope
// of what we cover, we can just take the first trace from the list,
// or otherwise just the first trace:
container = gd._fullData[binding.traces !== null ? binding.traces[0] : 0];
} else if(binding.type === 'layout') {
container = gd._fullLayout;
} else {
return false;
}
value = Lib.nestedProperty(container, binding.prop).get();
obj = cache[binding.type] = cache[binding.type] || {};
if(obj.hasOwnProperty(binding.prop)) {
if(obj[binding.prop] !== value) {
changed = true;
}
}
obj[binding.prop] = value;
return {
changed: changed,
value: value
};
}
/*
* Execute an API command. There's really not much to this; it just provides
* a common hook so that implementations don't need to be synchronized across
* multiple components with the ability to invoke API commands.
*
* @param {string} method
* The name of the plotly command to execute. Must be one of 'animate',
* 'restyle', 'relayout', 'update'.
* @param {array} args
* A list of arguments passed to the API command
*/
exports.executeAPICommand = function(gd, method, args) {
var apiMethod = Plotly[method];
var allArgs = [gd];
if(!Array.isArray(args)) args = [];
for(var i = 0; i < args.length; i++) {
allArgs.push(args[i]);
}
return apiMethod.apply(null, allArgs).catch(function(err) {
Lib.warn('API call to Plotly.' + method + ' rejected.', err);
return Promise.reject(err);
});
};
exports.computeAPICommandBindings = function(gd, method, args) {
var bindings;
if(!Array.isArray(args)) args = [];
switch(method) {
case 'restyle':
bindings = computeDataBindings(gd, args);
break;
case 'relayout':
bindings = computeLayoutBindings(gd, args);
break;
case 'update':
bindings = computeDataBindings(gd, [args[0], args[2]])
.concat(computeLayoutBindings(gd, [args[1]]));
break;
case 'animate':
bindings = computeAnimateBindings(gd, args);
break;
default:
// This is the case where intelligent logic about what affects
// this command is not implemented. It causes no ill effects.
// For example, addFrames simply won't bind to a control component.
bindings = [];
}
return bindings;
};
function computeAnimateBindings(gd, args) {
// We'll assume that the only relevant modification an animation
// makes that's meaningfully tracked is the frame:
if(Array.isArray(args[0]) && args[0].length === 1 && ['string', 'number'].indexOf(typeof args[0][0]) !== -1) {
return [{type: 'layout', prop: '_currentFrame', value: args[0][0].toString()}];
} else {
return [];
}
}
function computeLayoutBindings(gd, args) {
var bindings = [];
var astr = args[0];
var aobj = {};
if(typeof astr === 'string') {
aobj[astr] = args[1];
} else if(Lib.isPlainObject(astr)) {
aobj = astr;
} else {
return bindings;
}
crawl(aobj, function(path, attrName, attr) {
bindings.push({type: 'layout', prop: path, value: attr});
}, '', 0);
return bindings;
}
function computeDataBindings(gd, args) {
var traces, astr, val, aobj;
var bindings = [];
// Logic copied from Plotly.restyle:
astr = args[0];
val = args[1];
traces = args[2];
aobj = {};
if(typeof astr === 'string') {
aobj[astr] = val;
} else if(Lib.isPlainObject(astr)) {
// the 3-arg form
aobj = astr;
if(traces === undefined) {
traces = val;
}
} else {
return bindings;
}
if(traces === undefined) {
// Explicitly assign this to null instead of undefined:
traces = null;
}
crawl(aobj, function(path, attrName, attr) {
var thisTraces;
if(Array.isArray(attr)) {
var nAttr = Math.min(attr.length, gd.data.length);
if(traces) {
nAttr = Math.min(nAttr, traces.length);
}
thisTraces = [];
for(var j = 0; j < nAttr; j++) {
thisTraces[j] = traces ? traces[j] : j;
}
} else {
thisTraces = traces ? traces.slice(0) : null;
}
// Convert [7] to just 7 when traces is null:
if(thisTraces === null) {
if(Array.isArray(attr)) {
attr = attr[0];
}
} else if(Array.isArray(thisTraces)) {
if(!Array.isArray(attr)) {
var tmp = attr;
attr = [];
for(var i = 0; i < thisTraces.length; i++) {
attr[i] = tmp;
}
}
attr.length = Math.min(thisTraces.length, attr.length);
}
bindings.push({
type: 'data',
prop: path,
traces: thisTraces,
value: attr
});
}, '', 0);
return bindings;
}
function crawl(attrs, callback, path, depth) {
Object.keys(attrs).forEach(function(attrName) {
var attr = attrs[attrName];
if(attrName[0] === '_') return;
var thisPath = path + (depth > 0 ? '.' : '') + attrName;
if(Lib.isPlainObject(attr)) {
crawl(attr, callback, thisPath, depth + 1);
} else {
// Only execute the callback on leaf nodes:
callback(thisPath, attrName, attr);
}
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
family: {
valType: 'string',
role: 'style',
noBlank: true,
strict: true,
description: [
'HTML font family - the typeface that will be applied by the web browser.',
'The web browser will only be able to apply a font if it is available on the system',
'which it operates. Provide multiple font families, separated by commas, to indicate',
'the preference in which to apply fonts if they aren\'t available on the system.',
'The plotly service (at https://plot.ly or on-premise) generates images on a server,',
'where only a select number of',
'fonts are installed and supported.',
'These include *Arial*, *Balto*, *Courier New*, *Droid Sans*,, *Droid Serif*,',
'*Droid Sans Mono*, *Gravitas One*, *Old Standard TT*, *Open Sans*, *Overpass*,',
'*PT Sans Narrow*, *Raleway*, *Times New Roman*.'
].join(' ')
},
size: {
valType: 'number',
role: 'style',
min: 1
},
color: {
valType: 'color',
role: 'style'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
_isLinkedToArray: 'frames_entry',
group: {
valType: 'string',
role: 'info',
description: [
'An identifier that specifies the group to which the frame belongs,',
'used by animate to select a subset of frames.'
].join(' ')
},
name: {
valType: 'string',
role: 'info',
description: 'A label by which to identify the frame'
},
traces: {
valType: 'any',
role: 'info',
description: [
'A list of trace indices that identify the respective traces in the',
'data attribute'
].join(' ')
},
baseframe: {
valType: 'string',
role: 'info',
description: [
'The name of the frame into which this frame\'s properties are merged',
'before applying. This is used to unify properties and avoid needing',
'to specify the same values for the same properties in multiple frames.'
].join(' ')
},
data: {
valType: 'any',
role: 'object',
description: [
'A list of traces this frame modifies. The format is identical to the',
'normal trace definition.'
].join(' ')
},
layout: {
valType: 'any',
role: 'object',
description: [
'Layout properties which this frame modifies. The format is identical',
'to the normal layout definition.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var extendFlat = Lib.extendFlat;
var fontAttrs = require('./font_attributes');
var colorAttrs = require('../components/color/attributes');
module.exports = {
font: {
family: extendFlat({}, fontAttrs.family, {
dflt: '"Open Sans", verdana, arial, sans-serif'
}),
size: extendFlat({}, fontAttrs.size, {
dflt: 12
}),
color: extendFlat({}, fontAttrs.color, {
dflt: colorAttrs.defaultLine
}),
description: [
'Sets the global font.',
'Note that fonts used in traces and other',
'layout components inherit from the global font.'
].join(' ')
},
title: {
valType: 'string',
role: 'info',
dflt: 'Click to enter Plot title',
description: [
'Sets the plot\'s title.'
].join(' ')
},
titlefont: extendFlat({}, fontAttrs, {
description: 'Sets the title font.'
}),
autosize: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Determines whether or not a layout width or height',
'that has been left undefined by the user',
'is initialized on each relayout.',
'Note that, regardless of this attribute,',
'an undefined layout width or height',
'is always initialized on the first call to plot.'
].join(' ')
},
width: {
valType: 'number',
role: 'info',
min: 10,
dflt: 700,
description: [
'Sets the plot\'s width (in px).'
].join(' ')
},
height: {
valType: 'number',
role: 'info',
min: 10,
dflt: 450,
description: [
'Sets the plot\'s height (in px).'
].join(' ')
},
margin: {
l: {
valType: 'number',
role: 'info',
min: 0,
dflt: 80,
description: 'Sets the left margin (in px).'
},
r: {
valType: 'number',
role: 'info',
min: 0,
dflt: 80,
description: 'Sets the right margin (in px).'
},
t: {
valType: 'number',
role: 'info',
min: 0,
dflt: 100,
description: 'Sets the top margin (in px).'
},
b: {
valType: 'number',
role: 'info',
min: 0,
dflt: 80,
description: 'Sets the bottom margin (in px).'
},
pad: {
valType: 'number',
role: 'info',
min: 0,
dflt: 0,
description: [
'Sets the amount of padding (in px)',
'between the plotting area and the axis lines'
].join(' ')
},
autoexpand: {
valType: 'boolean',
role: 'info',
dflt: true
}
},
paper_bgcolor: {
valType: 'color',
role: 'style',
dflt: colorAttrs.background,
description: 'Sets the color of paper where the graph is drawn.'
},
plot_bgcolor: {
// defined here, but set in Axes.supplyLayoutDefaults
// because it needs to know if there are (2D) axes or not
valType: 'color',
role: 'style',
dflt: colorAttrs.background,
description: [
'Sets the color of plotting area in-between x and y axes.'
].join(' ')
},
separators: {
valType: 'string',
role: 'style',
dflt: '.,',
description: [
'Sets the decimal and thousand separators.',
'For example, *. * puts a \'.\' before decimals and',
'a space between thousands.'
].join(' ')
},
hidesources: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Determines whether or not a text link citing the data source is',
'placed at the bottom-right cored of the figure.',
'Has only an effect only on graphs that have been generated via',
'forked graphs from the plotly service (at https://plot.ly or on-premise).'
].join(' ')
},
smith: {
// will become a boolean if/when we implement this
valType: 'enumerated',
role: 'info',
values: [false],
dflt: false
},
showlegend: {
// handled in legend.supplyLayoutDefaults
// but included here because it's not in the legend object
valType: 'boolean',
role: 'info',
description: 'Determines whether or not a legend is drawn.'
},
dragmode: {
valType: 'enumerated',
role: 'info',
values: ['zoom', 'pan', 'select', 'lasso', 'orbit', 'turntable'],
dflt: 'zoom',
description: [
'Determines the mode of drag interactions.',
'*select* and *lasso* apply only to scatter traces with',
'markers or text. *orbit* and *turntable* apply only to',
'3D scenes.'
].join(' ')
},
hovermode: {
valType: 'enumerated',
role: 'info',
values: ['x', 'y', 'closest', false],
description: 'Determines the mode of hover interactions.'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
t: {
valType: 'number',
dflt: 0,
role: 'style',
description: 'The amount of padding (in px) along the top of the component.'
},
r: {
valType: 'number',
dflt: 0,
role: 'style',
description: 'The amount of padding (in px) on the right side of the component.'
},
b: {
valType: 'number',
dflt: 0,
role: 'style',
description: 'The amount of padding (in px) along the bottom of the component.'
},
l: {
valType: 'number',
dflt: 0,
role: 'style',
description: 'The amount of padding (in px) on the left side of the component.'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Plotly = require('../plotly');
var Registry = require('../registry');
var Lib = require('../lib');
var Color = require('../components/color');
var BADNUM = require('../constants/numerical').BADNUM;
var plots = module.exports = {};
var animationAttrs = require('./animation_attributes');
var frameAttrs = require('./frame_attributes');
var relinkPrivateKeys = Lib.relinkPrivateKeys;
// Expose registry methods on Plots for backward-compatibility
Lib.extendFlat(plots, Registry);
plots.attributes = require('./attributes');
plots.attributes.type.values = plots.allTypes;
plots.fontAttrs = require('./font_attributes');
plots.layoutAttributes = require('./layout_attributes');
// TODO make this a plot attribute?
plots.fontWeight = 'normal';
var subplotsRegistry = plots.subplotsRegistry;
var transformsRegistry = plots.transformsRegistry;
var ErrorBars = require('../components/errorbars');
var commandModule = require('./command');
plots.executeAPICommand = commandModule.executeAPICommand;
plots.computeAPICommandBindings = commandModule.computeAPICommandBindings;
plots.manageCommandObserver = commandModule.manageCommandObserver;
plots.hasSimpleAPICommandBindings = commandModule.hasSimpleAPICommandBindings;
/**
* Find subplot ids in data.
* Meant to be used in the defaults step.
*
* Use plots.getSubplotIds to grab the current
* subplot ids later on in Plotly.plot.
*
* @param {array} data plotly data array
* (intended to be _fullData, but does not have to be).
* @param {string} type subplot type to look for.
*
* @return {array} list of subplot ids (strings).
* N.B. these ids possibly un-ordered.
*
* TODO incorporate cartesian/gl2d axis finders in this paradigm.
*/
plots.findSubplotIds = function findSubplotIds(data, type) {
var subplotIds = [];
if(!plots.subplotsRegistry[type]) return subplotIds;
var attr = plots.subplotsRegistry[type].attr;
for(var i = 0; i < data.length; i++) {
var trace = data[i];
if(plots.traceIs(trace, type) && subplotIds.indexOf(trace[attr]) === -1) {
subplotIds.push(trace[attr]);
}
}
return subplotIds;
};
/**
* Get the ids of the current subplots.
*
* @param {object} layout plotly full layout object.
* @param {string} type subplot type to look for.
*
* @return {array} list of ordered subplot ids (strings).
*
*/
plots.getSubplotIds = function getSubplotIds(layout, type) {
var _module = plots.subplotsRegistry[type];
if(!_module) return [];
// layout must be 'fullLayout' here
if(type === 'cartesian' && (!layout._has || !layout._has('cartesian'))) return [];
if(type === 'gl2d' && (!layout._has || !layout._has('gl2d'))) return [];
if(type === 'cartesian' || type === 'gl2d') {
return Object.keys(layout._plots || {});
}
var idRegex = _module.idRegex,
layoutKeys = Object.keys(layout),
subplotIds = [];
for(var i = 0; i < layoutKeys.length; i++) {
var layoutKey = layoutKeys[i];
if(idRegex.test(layoutKey)) subplotIds.push(layoutKey);
}
// order the ids
var idLen = _module.idRoot.length;
subplotIds.sort(function(a, b) {
var aNum = +(a.substr(idLen) || 1),
bNum = +(b.substr(idLen) || 1);
return aNum - bNum;
});
return subplotIds;
};
/**
* Get the data trace(s) associated with a given subplot.
*
* @param {array} data plotly full data array.
* @param {string} type subplot type to look for.
* @param {string} subplotId subplot id to look for.
*
* @return {array} list of trace objects.
*
*/
plots.getSubplotData = function getSubplotData(data, type, subplotId) {
if(!plots.subplotsRegistry[type]) return [];
var attr = plots.subplotsRegistry[type].attr,
subplotData = [],
trace;
for(var i = 0; i < data.length; i++) {
trace = data[i];
if(type === 'gl2d' && plots.traceIs(trace, 'gl2d')) {
var spmatch = Plotly.Axes.subplotMatch,
subplotX = 'x' + subplotId.match(spmatch)[1],
subplotY = 'y' + subplotId.match(spmatch)[2];
if(trace[attr[0]] === subplotX && trace[attr[1]] === subplotY) {
subplotData.push(trace);
}
}
else {
if(trace[attr] === subplotId) subplotData.push(trace);
}
}
return subplotData;
};
/**
* Get calcdata traces(s) associated with a given subplot
*
* @param {array} calcData (as in gd.calcdata)
* @param {string} type subplot type
* @param {string} subplotId subplot id to look for
*
* @return {array} array of calcdata traces
*/
plots.getSubplotCalcData = function(calcData, type, subplotId) {
if(!plots.subplotsRegistry[type]) return [];
var attr = plots.subplotsRegistry[type].attr;
var subplotCalcData = [];
for(var i = 0; i < calcData.length; i++) {
var calcTrace = calcData[i],
trace = calcTrace[0].trace;
if(trace[attr] === subplotId) subplotCalcData.push(calcTrace);
}
return subplotCalcData;
};
// in some cases the browser doesn't seem to know how big
// the text is at first, so it needs to draw it,
// then wait a little, then draw it again
plots.redrawText = function(gd) {
// do not work if polar is present
if((gd.data && gd.data[0] && gd.data[0].r)) return;
return new Promise(function(resolve) {
setTimeout(function() {
Registry.getComponentMethod('annotations', 'draw')(gd);
Registry.getComponentMethod('legend', 'draw')(gd);
(gd.calcdata || []).forEach(function(d) {
if(d[0] && d[0].t && d[0].t.cb) d[0].t.cb();
});
resolve(plots.previousPromises(gd));
}, 300);
});
};
// resize plot about the container size
plots.resize = function(gd) {
return new Promise(function(resolve, reject) {
if(!gd || d3.select(gd).style('display') === 'none') {
reject(new Error('Resize must be passed a plot div element.'));
}
if(gd._redrawTimer) clearTimeout(gd._redrawTimer);
gd._redrawTimer = setTimeout(function() {
// return if there is nothing to resize
if(gd.layout.width && gd.layout.height) {
resolve(gd);
return;
}
delete gd.layout.width;
delete gd.layout.height;
// autosizing doesn't count as a change that needs saving
var oldchanged = gd.changed;
// nor should it be included in the undo queue
gd.autoplay = true;
Plotly.relayout(gd, { autosize: true }).then(function() {
gd.changed = oldchanged;
resolve(gd);
});
}, 100);
});
};
// for use in Lib.syncOrAsync, check if there are any
// pending promises in this plot and wait for them
plots.previousPromises = function(gd) {
if((gd._promises || []).length) {
return Promise.all(gd._promises)
.then(function() { gd._promises = []; });
}
};
/**
* Adds the 'Edit chart' link.
* Note that now Plotly.plot() calls this so it can regenerate whenever it replots
*
* Add source links to your graph inside the 'showSources' config argument.
*/
plots.addLinks = function(gd) {
// Do not do anything if showLink and showSources are not set to true in config
if(!gd._context.showLink && !gd._context.showSources) return;
var fullLayout = gd._fullLayout;
var linkContainer = fullLayout._paper
.selectAll('text.js-plot-link-container').data([0]);
linkContainer.enter().append('text')
.classed('js-plot-link-container', true)
.style({
'font-family': '"Open Sans", Arial, sans-serif',
'font-size': '12px',
'fill': Color.defaultLine,
'pointer-events': 'all'
})
.each(function() {
var links = d3.select(this);
links.append('tspan').classed('js-link-to-tool', true);
links.append('tspan').classed('js-link-spacer', true);
links.append('tspan').classed('js-sourcelinks', true);
});
// The text node inside svg
var text = linkContainer.node(),
attrs = {
y: fullLayout._paper.attr('height') - 9
};
// If text's width is bigger than the layout
// Check that text is a child node or document.body
// because otherwise IE/Edge might throw an exception
// when calling getComputedTextLength().
// Apparently offsetParent is null for invisibles.
if(document.body.contains(text) && text.getComputedTextLength() >= (fullLayout.width - 20)) {
// Align the text at the left
attrs['text-anchor'] = 'start';
attrs.x = 5;
}
else {
// Align the text at the right
attrs['text-anchor'] = 'end';
attrs.x = fullLayout._paper.attr('width') - 7;
}
linkContainer.attr(attrs);
var toolspan = linkContainer.select('.js-link-to-tool'),
spacespan = linkContainer.select('.js-link-spacer'),
sourcespan = linkContainer.select('.js-sourcelinks');
if(gd._context.showSources) gd._context.showSources(gd);
// 'view in plotly' link for embedded plots
if(gd._context.showLink) positionPlayWithData(gd, toolspan);
// separator if we have both sources and tool link
spacespan.text((toolspan.text() && sourcespan.text()) ? ' - ' : '');
};
// note that now this function is only adding the brand in
// iframes and 3rd-party apps
function positionPlayWithData(gd, container) {
container.text('');
var link = container.append('a')
.attr({
'xlink:xlink:href': '#',
'class': 'link--impt link--embedview',
'font-weight': 'bold'
})
.text(gd._context.linkText + ' ' + String.fromCharCode(187));
if(gd._context.sendData) {
link.on('click', function() {
plots.sendDataToCloud(gd);
});
}
else {
var path = window.location.pathname.split('/');
var query = window.location.search;
link.attr({
'xlink:xlink:show': 'new',
'xlink:xlink:href': '/' + path[2].split('.')[0] + '/' + path[1] + query
});
}
}
plots.sendDataToCloud = function(gd) {
gd.emit('plotly_beforeexport');
var baseUrl = (window.PLOTLYENV && window.PLOTLYENV.BASE_URL) || 'https://plot.ly';
var hiddenformDiv = d3.select(gd)
.append('div')
.attr('id', 'hiddenform')
.style('display', 'none');
var hiddenform = hiddenformDiv
.append('form')
.attr({
action: baseUrl + '/external',
method: 'post',
target: '_blank'
});
var hiddenformInput = hiddenform
.append('input')
.attr({
type: 'text',
name: 'data'
});
hiddenformInput.node().value = plots.graphJson(gd, false, 'keepdata');
hiddenform.node().submit();
hiddenformDiv.remove();
gd.emit('plotly_afterexport');
return false;
};
// Fill in default values:
//
// gd.data, gd.layout:
// are precisely what the user specified,
// these fields shouldn't be modified nor used directly
// after the supply defaults step.
//
// gd._fullData, gd._fullLayout:
// are complete descriptions of how to draw the plot,
// use these fields in all required computations.
//
// gd._fullLayout._modules
// is a list of all the trace modules required to draw the plot.
//
// gd._fullLayout._basePlotModules
// is a list of all the plot modules required to draw the plot.
//
// gd._fullLayout._transformModules
// is a list of all the transform modules invoked.
//
plots.supplyDefaults = function(gd) {
var oldFullLayout = gd._fullLayout || {},
newFullLayout = gd._fullLayout = {},
newLayout = gd.layout || {};
var oldFullData = gd._fullData || [],
newFullData = gd._fullData = [],
newData = gd.data || [];
var i;
// Create all the storage space for frames, but only if doesn't already exist
if(!gd._transitionData) plots.createTransitionData(gd);
// first fill in what we can of layout without looking at data
// because fullData needs a few things from layout
if(oldFullLayout._initialAutoSizeIsDone) {
// coerce the updated layout while preserving width and height
var oldWidth = oldFullLayout.width,
oldHeight = oldFullLayout.height;
plots.supplyLayoutGlobalDefaults(newLayout, newFullLayout);
if(!newLayout.width) newFullLayout.width = oldWidth;
if(!newLayout.height) newFullLayout.height = oldHeight;
}
else {
// coerce the updated layout and autosize if needed
plots.supplyLayoutGlobalDefaults(newLayout, newFullLayout);
var missingWidthOrHeight = (!newLayout.width || !newLayout.height),
autosize = newFullLayout.autosize,
autosizable = gd._context && gd._context.autosizable,
initialAutoSize = missingWidthOrHeight && (autosize || autosizable);
if(initialAutoSize) plots.plotAutoSize(gd, newLayout, newFullLayout);
else if(missingWidthOrHeight) plots.sanitizeMargins(gd);
// for backwards-compatibility with Plotly v1.x.x
if(!autosize && missingWidthOrHeight) {
newLayout.width = newFullLayout.width;
newLayout.height = newFullLayout.height;
}
}
newFullLayout._initialAutoSizeIsDone = true;
// keep track of how many traces are inputted
newFullLayout._dataLength = newData.length;
// then do the data
newFullLayout._globalTransforms = (gd._context || {}).globalTransforms;
plots.supplyDataDefaults(newData, newFullData, newLayout, newFullLayout);
// attach helper method to check whether a plot type is present on graph
newFullLayout._has = plots._hasPlotType.bind(newFullLayout);
// special cases that introduce interactions between traces
var _modules = newFullLayout._modules;
for(i = 0; i < _modules.length; i++) {
var _module = _modules[i];
if(_module.cleanData) _module.cleanData(newFullData);
}
if(oldFullData.length === newData.length) {
for(i = 0; i < newFullData.length; i++) {
relinkPrivateKeys(newFullData[i], oldFullData[i]);
}
}
// finally, fill in the pieces of layout that may need to look at data
plots.supplyLayoutModuleDefaults(newLayout, newFullLayout, newFullData, gd._transitionData);
// TODO remove in v2.0.0
// add has-plot-type refs to fullLayout for backward compatibility
newFullLayout._hasCartesian = newFullLayout._has('cartesian');
newFullLayout._hasGeo = newFullLayout._has('geo');
newFullLayout._hasGL3D = newFullLayout._has('gl3d');
newFullLayout._hasGL2D = newFullLayout._has('gl2d');
newFullLayout._hasTernary = newFullLayout._has('ternary');
newFullLayout._hasPie = newFullLayout._has('pie');
// clean subplots and other artifacts from previous plot calls
plots.cleanPlot(newFullData, newFullLayout, oldFullData, oldFullLayout);
// relink / initialize subplot axis objects
plots.linkSubplots(newFullData, newFullLayout, oldFullData, oldFullLayout);
// relink functions and _ attributes to promote consistency between plots
relinkPrivateKeys(newFullLayout, oldFullLayout);
// TODO may return a promise
plots.doAutoMargin(gd);
// set scale after auto margin routine
var axList = Plotly.Axes.list(gd);
for(i = 0; i < axList.length; i++) {
var ax = axList[i];
ax.setScale();
}
// update object references in calcdata
if((gd.calcdata || []).length === newFullData.length) {
for(i = 0; i < newFullData.length; i++) {
var trace = newFullData[i];
(gd.calcdata[i][0] || {}).trace = trace;
}
}
};
// Create storage for all of the data related to frames and transitions:
plots.createTransitionData = function(gd) {
// Set up the default keyframe if it doesn't exist:
if(!gd._transitionData) {
gd._transitionData = {};
}
if(!gd._transitionData._frames) {
gd._transitionData._frames = [];
}
if(!gd._transitionData._frameHash) {
gd._transitionData._frameHash = {};
}
if(!gd._transitionData._counter) {
gd._transitionData._counter = 0;
}
if(!gd._transitionData._interruptCallbacks) {
gd._transitionData._interruptCallbacks = [];
}
};
// helper function to be bound to fullLayout to check
// whether a certain plot type is present on plot
plots._hasPlotType = function(category) {
var basePlotModules = this._basePlotModules || [];
for(var i = 0; i < basePlotModules.length; i++) {
var _module = basePlotModules[i];
if(_module.name === category) return true;
}
return false;
};
plots.cleanPlot = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var i, j;
var basePlotModules = oldFullLayout._basePlotModules || [];
for(i = 0; i < basePlotModules.length; i++) {
var _module = basePlotModules[i];
if(_module.clean) {
_module.clean(newFullData, newFullLayout, oldFullData, oldFullLayout);
}
}
var hasPaper = !!oldFullLayout._paper;
var hasInfoLayer = !!oldFullLayout._infolayer;
oldLoop:
for(i = 0; i < oldFullData.length; i++) {
var oldTrace = oldFullData[i],
oldUid = oldTrace.uid;
for(j = 0; j < newFullData.length; j++) {
var newTrace = newFullData[j];
if(oldUid === newTrace.uid) continue oldLoop;
}
var query = (
'.hm' + oldUid +
',.contour' + oldUid +
',.carpet' + oldUid +
',#clip' + oldUid +
',.trace' + oldUid
);
// clean old heatmap, contour traces and clip paths
// that rely on uid identifiers
if(hasPaper) {
oldFullLayout._paper.selectAll(query).remove();
}
// clean old colorbars and range slider plot
if(hasInfoLayer) {
oldFullLayout._infolayer.selectAll('.cb' + oldUid).remove();
oldFullLayout._infolayer.selectAll('g.rangeslider-container')
.selectAll(query).remove();
}
}
};
plots.linkSubplots = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldSubplots = oldFullLayout._plots || {},
newSubplots = newFullLayout._plots = {};
var mockGd = {
_fullData: newFullData,
_fullLayout: newFullLayout
};
var ids = Plotly.Axes.getSubplots(mockGd);
for(var i = 0; i < ids.length; i++) {
var id = ids[i],
oldSubplot = oldSubplots[id],
plotinfo;
if(oldSubplot) {
plotinfo = newSubplots[id] = oldSubplot;
if(plotinfo._scene2d) {
plotinfo._scene2d.updateRefs(newFullLayout);
}
}
else {
plotinfo = newSubplots[id] = {};
plotinfo.id = id;
}
plotinfo.xaxis = Plotly.Axes.getFromId(mockGd, id, 'x');
plotinfo.yaxis = Plotly.Axes.getFromId(mockGd, id, 'y');
}
};
plots.supplyDataDefaults = function(dataIn, dataOut, layout, fullLayout) {
var i, fullTrace, trace;
var modules = fullLayout._modules = [],
basePlotModules = fullLayout._basePlotModules = [],
cnt = 0;
fullLayout._transformModules = [];
function pushModule(fullTrace) {
dataOut.push(fullTrace);
var _module = fullTrace._module;
if(!_module) return;
Lib.pushUnique(modules, _module);
Lib.pushUnique(basePlotModules, fullTrace._module.basePlotModule);
cnt++;
}
var carpetIndex = {};
var carpetDependents = [];
for(i = 0; i < dataIn.length; i++) {
trace = dataIn[i];
fullTrace = plots.supplyTraceDefaults(trace, cnt, fullLayout, i);
fullTrace.index = i;
fullTrace._input = trace;
fullTrace._expandedIndex = cnt;
if(fullTrace.transforms && fullTrace.transforms.length) {
var expandedTraces = applyTransforms(fullTrace, dataOut, layout, fullLayout);
for(var j = 0; j < expandedTraces.length; j++) {
var expandedTrace = expandedTraces[j],
fullExpandedTrace = plots.supplyTraceDefaults(expandedTrace, cnt, fullLayout, i);
// mutate uid here using parent uid and expanded index
// to promote consistency between update calls
expandedTrace.uid = fullExpandedTrace.uid = fullTrace.uid + j;
// add info about parent data trace
fullExpandedTrace.index = i;
fullExpandedTrace._input = trace;
fullExpandedTrace._fullInput = fullTrace;
// add info about the expanded data
fullExpandedTrace._expandedIndex = cnt;
fullExpandedTrace._expandedInput = expandedTrace;
pushModule(fullExpandedTrace);
}
}
else {
// add identify refs for consistency with transformed traces
fullTrace._fullInput = fullTrace;
fullTrace._expandedInput = fullTrace;
pushModule(fullTrace);
}
if(Registry.traceIs(fullTrace, 'carpetAxis')) {
carpetIndex[fullTrace.carpet] = fullTrace;
}
if(Registry.traceIs(fullTrace, 'carpetDependent')) {
carpetDependents.push(i);
}
}
for(i = 0; i < carpetDependents.length; i++) {
fullTrace = dataOut[carpetDependents[i]];
if(!fullTrace.visible) continue;
var carpetAxis = carpetIndex[fullTrace.carpet];
fullTrace._carpet = carpetAxis;
if(!carpetAxis || !carpetAxis.visible) {
fullTrace.visible = false;
continue;
}
fullTrace.xaxis = carpetAxis.xaxis;
fullTrace.yaxis = carpetAxis.yaxis;
}
};
plots.supplyAnimationDefaults = function(opts) {
opts = opts || {};
var i;
var optsOut = {};
function coerce(attr, dflt) {
return Lib.coerce(opts || {}, optsOut, animationAttrs, attr, dflt);
}
coerce('mode');
coerce('direction');
coerce('fromcurrent');
if(Array.isArray(opts.frame)) {
optsOut.frame = [];
for(i = 0; i < opts.frame.length; i++) {
optsOut.frame[i] = plots.supplyAnimationFrameDefaults(opts.frame[i] || {});
}
} else {
optsOut.frame = plots.supplyAnimationFrameDefaults(opts.frame || {});
}
if(Array.isArray(opts.transition)) {
optsOut.transition = [];
for(i = 0; i < opts.transition.length; i++) {
optsOut.transition[i] = plots.supplyAnimationTransitionDefaults(opts.transition[i] || {});
}
} else {
optsOut.transition = plots.supplyAnimationTransitionDefaults(opts.transition || {});
}
return optsOut;
};
plots.supplyAnimationFrameDefaults = function(opts) {
var optsOut = {};
function coerce(attr, dflt) {
return Lib.coerce(opts || {}, optsOut, animationAttrs.frame, attr, dflt);
}
coerce('duration');
coerce('redraw');
return optsOut;
};
plots.supplyAnimationTransitionDefaults = function(opts) {
var optsOut = {};
function coerce(attr, dflt) {
return Lib.coerce(opts || {}, optsOut, animationAttrs.transition, attr, dflt);
}
coerce('duration');
coerce('easing');
return optsOut;
};
plots.supplyFrameDefaults = function(frameIn) {
var frameOut = {};
function coerce(attr, dflt) {
return Lib.coerce(frameIn, frameOut, frameAttrs, attr, dflt);
}
coerce('group');
coerce('name');
coerce('traces');
coerce('baseframe');
coerce('data');
coerce('layout');
return frameOut;
};
plots.supplyTraceDefaults = function(traceIn, traceOutIndex, layout, traceInIndex) {
var traceOut = {},
defaultColor = Color.defaults[traceOutIndex % Color.defaults.length];
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, plots.attributes, attr, dflt);
}
function coerceSubplotAttr(subplotType, subplotAttr) {
if(!plots.traceIs(traceOut, subplotType)) return;
return Lib.coerce(traceIn, traceOut,
plots.subplotsRegistry[subplotType].attributes, subplotAttr);
}
var visible = coerce('visible');
coerce('type');
coerce('uid');
coerce('name', 'trace ' + traceInIndex);
// coerce subplot attributes of all registered subplot types
var subplotTypes = Object.keys(subplotsRegistry);
for(var i = 0; i < subplotTypes.length; i++) {
var subplotType = subplotTypes[i];
// done below (only when visible is true)
// TODO unified this pattern
if(['cartesian', 'gl2d'].indexOf(subplotType) !== -1) continue;
var attr = subplotsRegistry[subplotType].attr;
if(attr) coerceSubplotAttr(subplotType, attr);
}
if(visible) {
var _module = plots.getModule(traceOut);
traceOut._module = _module;
// gets overwritten in pie, geo and ternary modules
coerce('hoverinfo', (layout._dataLength === 1) ? 'x+y+z+text' : undefined);
if(plots.traceIs(traceOut, 'showLegend')) {
coerce('showlegend');
coerce('legendgroup');
}
// TODO add per-base-plot-module trace defaults step
if(_module) _module.supplyDefaults(traceIn, traceOut, defaultColor, layout);
if(!plots.traceIs(traceOut, 'noOpacity')) coerce('opacity');
coerceSubplotAttr('cartesian', 'xaxis');
coerceSubplotAttr('cartesian', 'yaxis');
coerceSubplotAttr('gl2d', 'xaxis');
coerceSubplotAttr('gl2d', 'yaxis');
if(plots.traceIs(traceOut, 'notLegendIsolatable')) {
// This clears out the legendonly state for traces like carpet that
// cannot be isolated in the legend
traceOut.visible = !!traceOut.visible;
}
plots.supplyTransformDefaults(traceIn, traceOut, layout);
}
return traceOut;
};
plots.supplyTransformDefaults = function(traceIn, traceOut, layout) {
var globalTransforms = layout._globalTransforms || [];
var transformModules = layout._transformModules || [];
if(!Array.isArray(traceIn.transforms) && globalTransforms.length === 0) return;
var containerIn = traceIn.transforms || [],
transformList = globalTransforms.concat(containerIn),
containerOut = traceOut.transforms = [];
for(var i = 0; i < transformList.length; i++) {
var transformIn = transformList[i],
type = transformIn.type,
_module = transformsRegistry[type],
transformOut;
if(!_module) Lib.warn('Unrecognized transform type ' + type + '.');
if(_module && _module.supplyDefaults) {
transformOut = _module.supplyDefaults(transformIn, traceOut, layout, traceIn);
transformOut.type = type;
transformOut._module = _module;
Lib.pushUnique(transformModules, _module);
}
else {
transformOut = Lib.extendFlat({}, transformIn);
}
containerOut.push(transformOut);
}
};
function applyTransforms(fullTrace, fullData, layout, fullLayout) {
var container = fullTrace.transforms,
dataOut = [fullTrace];
for(var i = 0; i < container.length; i++) {
var transform = container[i],
_module = transformsRegistry[transform.type];
if(_module && _module.transform) {
dataOut = _module.transform(dataOut, {
transform: transform,
fullTrace: fullTrace,
fullData: fullData,
layout: layout,
fullLayout: fullLayout,
transformIndex: i
});
}
}
return dataOut;
}
plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut) {
function coerce(attr, dflt) {
return Lib.coerce(layoutIn, layoutOut, plots.layoutAttributes, attr, dflt);
}
var globalFont = Lib.coerceFont(coerce, 'font');
coerce('title');
Lib.coerceFont(coerce, 'titlefont', {
family: globalFont.family,
size: Math.round(globalFont.size * 1.4),
color: globalFont.color
});
// Make sure that autosize is defaulted to *true*
// on layouts with no set width and height for backward compatibly,
// in particular https://plot.ly/javascript/responsive-fluid-layout/
//
// Before https://github.com/plotly/plotly.js/pull/635 ,
// layouts with no set width and height were set temporary set to 'initial'
// to pass through the autosize routine
//
// This behavior is subject to change in v2.
coerce('autosize', !(layoutIn.width && layoutIn.height));
coerce('width');
coerce('height');
coerce('margin.l');
coerce('margin.r');
coerce('margin.t');
coerce('margin.b');
coerce('margin.pad');
coerce('margin.autoexpand');
if(layoutIn.width && layoutIn.height) plots.sanitizeMargins(layoutOut);
coerce('paper_bgcolor');
coerce('separators');
coerce('hidesources');
coerce('smith');
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(layoutIn, layoutOut, 'calendar');
};
plots.plotAutoSize = function plotAutoSize(gd, layout, fullLayout) {
var context = gd._context || {},
frameMargins = context.frameMargins,
newWidth,
newHeight;
var isPlotDiv = Lib.isPlotDiv(gd);
if(isPlotDiv) gd.emit('plotly_autosize');
// embedded in an iframe - just take the full iframe size
// if we get to this point, with no aspect ratio restrictions
if(context.fillFrame) {
newWidth = window.innerWidth;
newHeight = window.innerHeight;
// somehow we get a few extra px height sometimes...
// just hide it
document.body.style.overflow = 'hidden';
}
else if(isNumeric(frameMargins) && frameMargins > 0) {
var reservedMargins = calculateReservedMargins(gd._boundingBoxMargins),
reservedWidth = reservedMargins.left + reservedMargins.right,
reservedHeight = reservedMargins.bottom + reservedMargins.top,
factor = 1 - 2 * frameMargins;
var gdBB = fullLayout._container && fullLayout._container.node ?
fullLayout._container.node().getBoundingClientRect() : {
width: fullLayout.width,
height: fullLayout.height
};
newWidth = Math.round(factor * (gdBB.width - reservedWidth));
newHeight = Math.round(factor * (gdBB.height - reservedHeight));
}
else {
// plotly.js - let the developers do what they want, either
// provide height and width for the container div,
// specify size in layout, or take the defaults,
// but don't enforce any ratio restrictions
var computedStyle = isPlotDiv ? window.getComputedStyle(gd) : {};
newWidth = parseFloat(computedStyle.width) || fullLayout.width;
newHeight = parseFloat(computedStyle.height) || fullLayout.height;
}
var minWidth = plots.layoutAttributes.width.min,
minHeight = plots.layoutAttributes.height.min;
if(newWidth < minWidth) newWidth = minWidth;
if(newHeight < minHeight) newHeight = minHeight;
var widthHasChanged = !layout.width &&
(Math.abs(fullLayout.width - newWidth) > 1),
heightHasChanged = !layout.height &&
(Math.abs(fullLayout.height - newHeight) > 1);
if(heightHasChanged || widthHasChanged) {
if(widthHasChanged) fullLayout.width = newWidth;
if(heightHasChanged) fullLayout.height = newHeight;
}
// cache initial autosize value, used in relayout when
// width or height values are set to null
if(!gd._initialAutoSize) {
gd._initialAutoSize = { width: newWidth, height: newHeight };
}
plots.sanitizeMargins(fullLayout);
};
/**
* Reduce all reserved margin objects to a single required margin reservation.
*
* @param {Object} margins
* @returns {{left: number, right: number, bottom: number, top: number}}
*/
function calculateReservedMargins(margins) {
var resultingMargin = {left: 0, right: 0, bottom: 0, top: 0},
marginName;
if(margins) {
for(marginName in margins) {
if(margins.hasOwnProperty(marginName)) {
resultingMargin.left += margins[marginName].left || 0;
resultingMargin.right += margins[marginName].right || 0;
resultingMargin.bottom += margins[marginName].bottom || 0;
resultingMargin.top += margins[marginName].top || 0;
}
}
}
return resultingMargin;
}
plots.supplyLayoutModuleDefaults = function(layoutIn, layoutOut, fullData, transitionData) {
var i, _module;
// can't be be part of basePlotModules loop
// in order to handle the orphan axes case
Plotly.Axes.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
// base plot module layout defaults
var basePlotModules = layoutOut._basePlotModules;
for(i = 0; i < basePlotModules.length; i++) {
_module = basePlotModules[i];
// done above already
if(_module.name === 'cartesian') continue;
// e.g. gl2d does not have a layout-defaults step
if(_module.supplyLayoutDefaults) {
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
}
}
// trace module layout defaults
var modules = layoutOut._modules;
for(i = 0; i < modules.length; i++) {
_module = modules[i];
if(_module.supplyLayoutDefaults) {
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
}
}
// transform module layout defaults
var transformModules = layoutOut._transformModules;
for(i = 0; i < transformModules.length; i++) {
_module = transformModules[i];
if(_module.supplyLayoutDefaults) {
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData, transitionData);
}
}
// should FX be a component?
Plotly.Fx.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
var components = Object.keys(Registry.componentsRegistry);
for(i = 0; i < components.length; i++) {
_module = Registry.componentsRegistry[components[i]];
if(_module.supplyLayoutDefaults) {
_module.supplyLayoutDefaults(layoutIn, layoutOut, fullData);
}
}
};
// Remove all plotly attributes from a div so it can be replotted fresh
// TODO: these really need to be encapsulated into a much smaller set...
plots.purge = function(gd) {
// note: we DO NOT remove _context because it doesn't change when we insert
// a new plot, and may have been set outside of our scope.
var fullLayout = gd._fullLayout || {};
if(fullLayout._glcontainer !== undefined) fullLayout._glcontainer.remove();
if(fullLayout._geocontainer !== undefined) fullLayout._geocontainer.remove();
// remove modebar
if(fullLayout._modeBar) fullLayout._modeBar.destroy();
if(gd._transitionData) {
// Ensure any dangling callbacks are simply dropped if the plot is purged.
// This is more or less only actually important for testing.
if(gd._transitionData._interruptCallbacks) {
gd._transitionData._interruptCallbacks.length = 0;
}
if(gd._transitionData._animationRaf) {
window.cancelAnimationFrame(gd._transitionData._animationRaf);
}
}
// data and layout
delete gd.data;
delete gd.layout;
delete gd._fullData;
delete gd._fullLayout;
delete gd.calcdata;
delete gd.framework;
delete gd.empty;
delete gd.fid;
delete gd.undoqueue; // action queue
delete gd.undonum;
delete gd.autoplay; // are we doing an action that doesn't go in undo queue?
delete gd.changed;
// these get recreated on Plotly.plot anyway, but just to be safe
// (and to have a record of them...)
delete gd._tester;
delete gd._testref;
delete gd._promises;
delete gd._redrawTimer;
delete gd.firstscatter;
delete gd.hmlumcount;
delete gd.hmpixcount;
delete gd.numboxes;
delete gd._hoverTimer;
delete gd._lastHoverTime;
delete gd._transitionData;
delete gd._transitioning;
delete gd._initialAutoSize;
// remove all event listeners
if(gd.removeAllListeners) gd.removeAllListeners();
};
plots.style = function(gd) {
var _modules = gd._fullLayout._modules;
for(var i = 0; i < _modules.length; i++) {
var _module = _modules[i];
if(_module.style) _module.style(gd);
}
};
plots.sanitizeMargins = function(fullLayout) {
// polar doesn't do margins...
if(!fullLayout || !fullLayout.margin) return;
var width = fullLayout.width,
height = fullLayout.height,
margin = fullLayout.margin,
plotWidth = width - (margin.l + margin.r),
plotHeight = height - (margin.t + margin.b),
correction;
// if margin.l + margin.r = 0 then plotWidth > 0
// as width >= 10 by supplyDefaults
// similarly for margin.t + margin.b
if(plotWidth < 0) {
correction = (width - 1) / (margin.l + margin.r);
margin.l = Math.floor(correction * margin.l);
margin.r = Math.floor(correction * margin.r);
}
if(plotHeight < 0) {
correction = (height - 1) / (margin.t + margin.b);
margin.t = Math.floor(correction * margin.t);
margin.b = Math.floor(correction * margin.b);
}
};
// called by components to see if we need to
// expand the margins to show them
// o is {x,l,r,y,t,b} where x and y are plot fractions,
// the rest are pixels in each direction
// or leave o out to delete this entry (like if it's hidden)
plots.autoMargin = function(gd, id, o) {
var fullLayout = gd._fullLayout;
if(!fullLayout._pushmargin) fullLayout._pushmargin = {};
if(fullLayout.margin.autoexpand !== false) {
if(!o) delete fullLayout._pushmargin[id];
else {
var pad = o.pad === undefined ? 12 : o.pad;
// if the item is too big, just give it enough automargin to
// make sure you can still grab it and bring it back
if(o.l + o.r > fullLayout.width * 0.5) o.l = o.r = 0;
if(o.b + o.t > fullLayout.height * 0.5) o.b = o.t = 0;
fullLayout._pushmargin[id] = {
l: {val: o.x, size: o.l + pad},
r: {val: o.x, size: o.r + pad},
b: {val: o.y, size: o.b + pad},
t: {val: o.y, size: o.t + pad}
};
}
if(!fullLayout._replotting) plots.doAutoMargin(gd);
}
};
plots.doAutoMargin = function(gd) {
var fullLayout = gd._fullLayout;
if(!fullLayout._size) fullLayout._size = {};
if(!fullLayout._pushmargin) fullLayout._pushmargin = {};
var gs = fullLayout._size,
oldmargins = JSON.stringify(gs);
// adjust margins for outside components
// fullLayout.margin is the requested margin,
// fullLayout._size has margins and plotsize after adjustment
var ml = Math.max(fullLayout.margin.l || 0, 0),
mr = Math.max(fullLayout.margin.r || 0, 0),
mt = Math.max(fullLayout.margin.t || 0, 0),
mb = Math.max(fullLayout.margin.b || 0, 0),
pm = fullLayout._pushmargin;
if(fullLayout.margin.autoexpand !== false) {
// fill in the requested margins
pm.base = {
l: {val: 0, size: ml},
r: {val: 1, size: mr},
t: {val: 1, size: mt},
b: {val: 0, size: mb}
};
// now cycle through all the combinations of l and r
// (and t and b) to find the required margins
var pmKeys = Object.keys(pm);
for(var i = 0; i < pmKeys.length; i++) {
var k1 = pmKeys[i];
var pushleft = pm[k1].l || {},
pushbottom = pm[k1].b || {},
fl = pushleft.val,
pl = pushleft.size,
fb = pushbottom.val,
pb = pushbottom.size;
for(var j = 0; j < pmKeys.length; j++) {
var k2 = pmKeys[j];
if(isNumeric(pl) && pm[k2].r) {
var fr = pm[k2].r.val,
pr = pm[k2].r.size;
if(fr > fl) {
var newl = (pl * fr +
(pr - fullLayout.width) * fl) / (fr - fl),
newr = (pr * (1 - fl) +
(pl - fullLayout.width) * (1 - fr)) / (fr - fl);
if(newl >= 0 && newr >= 0 && newl + newr > ml + mr) {
ml = newl;
mr = newr;
}
}
}
if(isNumeric(pb) && pm[k2].t) {
var ft = pm[k2].t.val,
pt = pm[k2].t.size;
if(ft > fb) {
var newb = (pb * ft +
(pt - fullLayout.height) * fb) / (ft - fb),
newt = (pt * (1 - fb) +
(pb - fullLayout.height) * (1 - ft)) / (ft - fb);
if(newb >= 0 && newt >= 0 && newb + newt > mb + mt) {
mb = newb;
mt = newt;
}
}
}
}
}
}
gs.l = Math.round(ml);
gs.r = Math.round(mr);
gs.t = Math.round(mt);
gs.b = Math.round(mb);
gs.p = Math.round(fullLayout.margin.pad);
gs.w = Math.round(fullLayout.width) - gs.l - gs.r;
gs.h = Math.round(fullLayout.height) - gs.t - gs.b;
// if things changed and we're not already redrawing, trigger a redraw
if(!fullLayout._replotting && oldmargins !== '{}' &&
oldmargins !== JSON.stringify(fullLayout._size)) {
return Plotly.plot(gd);
}
};
/**
* JSONify the graph data and layout
*
* This function needs to recurse because some src can be inside
* sub-objects.
*
* It also strips out functions and private (starts with _) elements.
* Therefore, we can add temporary things to data and layout that don't
* get saved.
*
* @param gd The graphDiv
* @param {Boolean} dataonly If true, don't return layout.
* @param {'keepref'|'keepdata'|'keepall'} [mode='keepref'] Filter what's kept
* keepref: remove data for which there's a src present
* eg if there's xsrc present (and xsrc is well-formed,
* ie has : and some chars before it), strip out x
* keepdata: remove all src tags, don't remove the data itself
* keepall: keep data and src
* @param {String} output If you specify 'object', the result will not be stringified
* @param {Boolean} useDefaults If truthy, use _fullLayout and _fullData
* @returns {Object|String}
*/
plots.graphJson = function(gd, dataonly, mode, output, useDefaults) {
// if the defaults aren't supplied yet, we need to do that...
if((useDefaults && dataonly && !gd._fullData) ||
(useDefaults && !dataonly && !gd._fullLayout)) {
plots.supplyDefaults(gd);
}
var data = (useDefaults) ? gd._fullData : gd.data,
layout = (useDefaults) ? gd._fullLayout : gd.layout,
frames = (gd._transitionData || {})._frames;
function stripObj(d) {
if(typeof d === 'function') {
return null;
}
if(Lib.isPlainObject(d)) {
var o = {}, v, src;
for(v in d) {
// remove private elements and functions
// _ is for private, [ is a mistake ie [object Object]
if(typeof d[v] === 'function' ||
['_', '['].indexOf(v.charAt(0)) !== -1) {
continue;
}
// look for src/data matches and remove the appropriate one
if(mode === 'keepdata') {
// keepdata: remove all ...src tags
if(v.substr(v.length - 3) === 'src') {
continue;
}
}
else if(mode === 'keepstream') {
// keep sourced data if it's being streamed.
// similar to keepref, but if the 'stream' object exists
// in a trace, we will keep the data array.
src = d[v + 'src'];
if(typeof src === 'string' && src.indexOf(':') > 0) {
if(!Lib.isPlainObject(d.stream)) {
continue;
}
}
}
else if(mode !== 'keepall') {
// keepref: remove sourced data but only
// if the source tag is well-formed
src = d[v + 'src'];
if(typeof src === 'string' && src.indexOf(':') > 0) {
continue;
}
}
// OK, we're including this... recurse into it
o[v] = stripObj(d[v]);
}
return o;
}
if(Array.isArray(d)) {
return d.map(stripObj);
}
// convert native dates to date strings...
// mostly for external users exporting to plotly
if(Lib.isJSDate(d)) return Lib.ms2DateTimeLocal(+d);
return d;
}
var obj = {
data: (data || []).map(function(v) {
var d = stripObj(v);
// fit has some little arrays in it that don't contain data,
// just fit params and meta
if(dataonly) { delete d.fit; }
return d;
})
};
if(!dataonly) { obj.layout = stripObj(layout); }
if(gd.framework && gd.framework.isPolar) obj = gd.framework.getConfig();
if(frames) obj.frames = stripObj(frames);
return (output === 'object') ? obj : JSON.stringify(obj);
};
/**
* Modify a keyframe using a list of operations:
*
* @param {array of objects} operations
* Sequence of operations to be performed on the keyframes
*/
plots.modifyFrames = function(gd, operations) {
var i, op, frame;
var _frames = gd._transitionData._frames;
var _hash = gd._transitionData._frameHash;
for(i = 0; i < operations.length; i++) {
op = operations[i];
switch(op.type) {
// No reason this couldn't exist, but is currently unused/untested:
/* case 'rename':
frame = _frames[op.index];
delete _hash[frame.name];
_hash[op.name] = frame;
frame.name = op.name;
break;*/
case 'replace':
frame = op.value;
var oldName = (_frames[op.index] || {}).name;
var newName = frame.name;
_frames[op.index] = _hash[newName] = frame;
if(newName !== oldName) {
// If name has changed in addition to replacement, then update
// the lookup table:
delete _hash[oldName];
_hash[newName] = frame;
}
break;
case 'insert':
frame = op.value;
_hash[frame.name] = frame;
_frames.splice(op.index, 0, frame);
break;
case 'delete':
frame = _frames[op.index];
delete _hash[frame.name];
_frames.splice(op.index, 1);
break;
}
}
return Promise.resolve();
};
/*
* Compute a keyframe. Merge a keyframe into its base frame(s) and
* expand properties.
*
* @param {object} frameLookup
* An object containing frames keyed by name (i.e. gd._transitionData._frameHash)
* @param {string} frame
* The name of the keyframe to be computed
*
* Returns: a new object with the merged content
*/
plots.computeFrame = function(gd, frameName) {
var frameLookup = gd._transitionData._frameHash;
var i, traceIndices, traceIndex, destIndex;
// Null or undefined will fail on .toString(). We'll allow numbers since we
// make it clear frames must be given string names, but we'll allow numbers
// here since they're otherwise fine for looking up frames as long as they're
// properly cast to strings. We really just want to ensure here that this
// 1) doesn't fail, and
// 2) doens't give an incorrect answer (which String(frameName) would)
if(!frameName) {
throw new Error('computeFrame must be given a string frame name');
}
var framePtr = frameLookup[frameName.toString()];
// Return false if the name is invalid:
if(!framePtr) {
return false;
}
var frameStack = [framePtr];
var frameNameStack = [framePtr.name];
// Follow frame pointers:
while(framePtr.baseframe && (framePtr = frameLookup[framePtr.baseframe.toString()])) {
// Avoid infinite loops:
if(frameNameStack.indexOf(framePtr.name) !== -1) break;
frameStack.push(framePtr);
frameNameStack.push(framePtr.name);
}
// A new object for the merged result:
var result = {};
// Merge, starting with the last and ending with the desired frame:
while((framePtr = frameStack.pop())) {
if(framePtr.layout) {
result.layout = plots.extendLayout(result.layout, framePtr.layout);
}
if(framePtr.data) {
if(!result.data) {
result.data = [];
}
traceIndices = framePtr.traces;
if(!traceIndices) {
// If not defined, assume serial order starting at zero
traceIndices = [];
for(i = 0; i < framePtr.data.length; i++) {
traceIndices[i] = i;
}
}
if(!result.traces) {
result.traces = [];
}
for(i = 0; i < framePtr.data.length; i++) {
// Loop through this frames data, find out where it should go,
// and merge it!
traceIndex = traceIndices[i];
if(traceIndex === undefined || traceIndex === null) {
continue;
}
destIndex = result.traces.indexOf(traceIndex);
if(destIndex === -1) {
destIndex = result.data.length;
result.traces[destIndex] = traceIndex;
}
result.data[destIndex] = plots.extendTrace(result.data[destIndex], framePtr.data[i]);
}
}
}
return result;
};
/*
* Recompute the lookup table that maps frame name -> frame object. addFrames/
* deleteFrames already manages this data one at a time, so the only time this
* is necessary is if you poke around manually in `gd._transitionData._frames`
* and create and haven't updated the lookup table.
*/
plots.recomputeFrameHash = function(gd) {
var hash = gd._transitionData._frameHash = {};
var frames = gd._transitionData._frames;
for(var i = 0; i < frames.length; i++) {
var frame = frames[i];
if(frame && frame.name) {
hash[frame.name] = frame;
}
}
};
/**
* Extend an object, treating container arrays very differently by extracting
* their contents and merging them separately.
*
* This exists so that we can extendDeepNoArrays and avoid stepping into data
* arrays without knowledge of the plot schema, but so that we may also manually
* recurse into known container arrays, such as transforms.
*
* See extendTrace and extendLayout below for usage.
*/
plots.extendObjectWithContainers = function(dest, src, containerPaths) {
var containerProp, containerVal, i, j, srcProp, destProp, srcContainer, destContainer;
var copy = Lib.extendDeepNoArrays({}, src || {});
var expandedObj = Lib.expandObjectPaths(copy);
var containerObj = {};
// Step through and extract any container properties. Otherwise extendDeepNoArrays
// will clobber any existing properties with an empty array and then supplyDefaults
// will reset everything to defaults.
if(containerPaths && containerPaths.length) {
for(i = 0; i < containerPaths.length; i++) {
containerProp = Lib.nestedProperty(expandedObj, containerPaths[i]);
containerVal = containerProp.get();
if(containerVal === undefined) {
Lib.nestedProperty(containerObj, containerPaths[i]).set(null);
}
else {
containerProp.set(null);
Lib.nestedProperty(containerObj, containerPaths[i]).set(containerVal);
}
}
}
dest = Lib.extendDeepNoArrays(dest || {}, expandedObj);
if(containerPaths && containerPaths.length) {
for(i = 0; i < containerPaths.length; i++) {
srcProp = Lib.nestedProperty(containerObj, containerPaths[i]);
srcContainer = srcProp.get();
if(!srcContainer) continue;
destProp = Lib.nestedProperty(dest, containerPaths[i]);
destContainer = destProp.get();
if(!Array.isArray(destContainer)) {
destContainer = [];
destProp.set(destContainer);
}
for(j = 0; j < srcContainer.length; j++) {
var srcObj = srcContainer[j];
if(srcObj === null) destContainer[j] = null;
else {
destContainer[j] = plots.extendObjectWithContainers(destContainer[j], srcObj);
}
}
destProp.set(destContainer);
}
}
return dest;
};
plots.dataArrayContainers = ['transforms'];
plots.layoutArrayContainers = Registry.layoutArrayContainers;
/*
* Extend a trace definition. This method:
*
* 1. directly transfers any array references
* 2. manually recurses into container arrays like transforms
*
* The result is the original object reference with the new contents merged in.
*/
plots.extendTrace = function(destTrace, srcTrace) {
return plots.extendObjectWithContainers(destTrace, srcTrace, plots.dataArrayContainers);
};
/*
* Extend a layout definition. This method:
*
* 1. directly transfers any array references (not critically important for
* layout since there aren't really data arrays)
* 2. manually recurses into container arrays like annotations
*
* The result is the original object reference with the new contents merged in.
*/
plots.extendLayout = function(destLayout, srcLayout) {
return plots.extendObjectWithContainers(destLayout, srcLayout, plots.layoutArrayContainers);
};
/**
* Transition to a set of new data and layout properties
*
* @param {DOM element} gd
* the DOM element of the graph container div
* @param {Object[]} data
* an array of data objects following the normal Plotly data definition format
* @param {Object} layout
* a layout object, following normal Plotly layout format
* @param {Number[]} traces
* indices of the corresponding traces specified in `data`
* @param {Object} frameOpts
* options for the frame (i.e. whether to redraw post-transition)
* @param {Object} transitionOpts
* options for the transition
*/
plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts) {
var i, traceIdx;
var dataLength = Array.isArray(data) ? data.length : 0;
var traceIndices = traces.slice(0, dataLength);
var transitionedTraces = [];
function prepareTransitions() {
var i;
for(i = 0; i < traceIndices.length; i++) {
var traceIdx = traceIndices[i];
var trace = gd._fullData[traceIdx];
var module = trace._module;
// There's nothing to do if this module is not defined:
if(!module) continue;
// Don't register the trace as transitioned if it doens't know what to do.
// If it *is* registered, it will receive a callback that it's responsible
// for calling in order to register the transition as having completed.
if(module.animatable) {
transitionedTraces.push(traceIdx);
}
gd.data[traceIndices[i]] = plots.extendTrace(gd.data[traceIndices[i]], data[i]);
}
// Follow the same procedure. Clone it so we don't mangle the input, then
// expand any object paths so we can merge deep into gd.layout:
var layoutUpdate = Lib.expandObjectPaths(Lib.extendDeepNoArrays({}, layout));
// Before merging though, we need to modify the incoming layout. We only
// know how to *transition* layout ranges, so it's imperative that a new
// range not be sent to the layout before the transition has started. So
// we must remove the things we can transition:
var axisAttrRe = /^[xy]axis[0-9]*$/;
for(var attr in layoutUpdate) {
if(!axisAttrRe.test(attr)) continue;
delete layoutUpdate[attr].range;
}
plots.extendLayout(gd.layout, layoutUpdate);
// Supply defaults after applying the incoming properties. Note that any attempt
// to simplify this step and reduce the amount of work resulted in the reconstruction
// of essentially the whole supplyDefaults step, so that it seems sensible to just use
// supplyDefaults even though it's heavier than would otherwise be desired for
// transitions:
plots.supplyDefaults(gd);
plots.doCalcdata(gd);
ErrorBars.calc(gd);
return Promise.resolve();
}
function executeCallbacks(list) {
var p = Promise.resolve();
if(!list) return p;
while(list.length) {
p = p.then((list.shift()));
}
return p;
}
function flushCallbacks(list) {
if(!list) return;
while(list.length) {
list.shift();
}
}
var aborted = false;
function executeTransitions() {
gd.emit('plotly_transitioning', []);
return new Promise(function(resolve) {
// This flag is used to disabled things like autorange:
gd._transitioning = true;
// When instantaneous updates are coming through quickly, it's too much to simply disable
// all interaction, so store this flag so we can disambiguate whether mouse interactions
// should be fully disabled or not:
if(transitionOpts.duration > 0) {
gd._transitioningWithDuration = true;
}
// If another transition is triggered, this callback will be executed simply because it's
// in the interruptCallbacks queue. If this transition completes, it will instead flush
// that queue and forget about this callback.
gd._transitionData._interruptCallbacks.push(function() {
aborted = true;
});
if(frameOpts.redraw) {
gd._transitionData._interruptCallbacks.push(function() {
return Plotly.redraw(gd);
});
}
// Emit this and make sure it happens last:
gd._transitionData._interruptCallbacks.push(function() {
gd.emit('plotly_transitioninterrupted', []);
});
// Construct callbacks that are executed on transition end. This ensures the d3 transitions
// are *complete* before anything else is done.
var numCallbacks = 0;
var numCompleted = 0;
function makeCallback() {
numCallbacks++;
return function() {
numCompleted++;
// When all are complete, perform a redraw:
if(!aborted && numCompleted === numCallbacks) {
completeTransition(resolve);
}
};
}
var traceTransitionOpts;
var j;
var basePlotModules = gd._fullLayout._basePlotModules;
var hasAxisTransition = false;
if(layout) {
for(j = 0; j < basePlotModules.length; j++) {
if(basePlotModules[j].transitionAxes) {
var newLayout = Lib.expandObjectPaths(layout);
hasAxisTransition = basePlotModules[j].transitionAxes(gd, newLayout, transitionOpts, makeCallback) || hasAxisTransition;
}
}
}
// Here handle the exception that we refuse to animate scales and axes at the same
// time. In other words, if there's an axis transition, then set the data transition
// to instantaneous.
if(hasAxisTransition) {
traceTransitionOpts = Lib.extendFlat({}, transitionOpts);
traceTransitionOpts.duration = 0;
} else {
traceTransitionOpts = transitionOpts;
}
for(j = 0; j < basePlotModules.length; j++) {
// Note that we pass a callback to *create* the callback that must be invoked on completion.
// This is since not all traces know about transitions, so it greatly simplifies matters if
// the trace is responsible for creating a callback, if needed, and then executing it when
// the time is right.
basePlotModules[j].plot(gd, transitionedTraces, traceTransitionOpts, makeCallback);
}
// If nothing else creates a callback, then this will trigger the completion in the next tick:
setTimeout(makeCallback());
});
}
function completeTransition(callback) {
// This a simple workaround for tests which purge the graph before animations
// have completed. That's not a very common case, so this is the simplest
// fix.
if(!gd._transitionData) return;
flushCallbacks(gd._transitionData._interruptCallbacks);
return Promise.resolve().then(function() {
if(frameOpts.redraw) {
return Plotly.redraw(gd);
}
}).then(function() {
// Set transitioning false again once the redraw has occurred. This is used, for example,
// to prevent the trailing redraw from autoranging:
gd._transitioning = false;
gd._transitioningWithDuration = false;
gd.emit('plotly_transitioned', []);
}).then(callback);
}
function interruptPreviousTransitions() {
// Fail-safe against purged plot:
if(!gd._transitionData) return;
// If a transition is interrupted, set this to false. At the moment, the only thing that would
// interrupt a transition is another transition, so that it will momentarily be set to true
// again, but this determines whether autorange or dragbox work, so it's for the sake of
// cleanliness:
gd._transitioning = false;
return executeCallbacks(gd._transitionData._interruptCallbacks);
}
for(i = 0; i < traceIndices.length; i++) {
traceIdx = traceIndices[i];
var contFull = gd._fullData[traceIdx];
var module = contFull._module;
if(!module) continue;
if(!module.animatable) {
var thisUpdate = {};
for(var ai in data[i]) {
thisUpdate[ai] = [data[i][ai]];
}
}
}
var seq = [plots.previousPromises, interruptPreviousTransitions, prepareTransitions, plots.rehover, executeTransitions];
var transitionStarting = Lib.syncOrAsync(seq, gd);
if(!transitionStarting || !transitionStarting.then) {
transitionStarting = Promise.resolve();
}
return transitionStarting.then(function() {
return gd;
});
};
plots.doCalcdata = function(gd, traces) {
var axList = Plotly.Axes.list(gd),
fullData = gd._fullData,
fullLayout = gd._fullLayout;
var trace, _module, i, j;
var hasCategoryAxis = false;
// XXX: Is this correct? Needs a closer look so that *some* traces can be recomputed without
// *all* needing doCalcdata:
var calcdata = new Array(fullData.length);
var oldCalcdata = (gd.calcdata || []).slice(0);
gd.calcdata = calcdata;
// extra helper variables
// firstscatter: fill-to-next on the first trace goes to zero
gd.firstscatter = true;
// how many box plots do we have (in case they're grouped)
gd.numboxes = 0;
// for calculating avg luminosity of heatmaps
gd._hmpixcount = 0;
gd._hmlumcount = 0;
// for sharing colors across pies (and for legend)
fullLayout._piecolormap = {};
fullLayout._piedefaultcolorcount = 0;
// initialize the category list, if there is one, so we start over
// to be filled in later by ax.d2c
for(i = 0; i < axList.length; i++) {
axList[i]._categories = axList[i]._initialCategories.slice();
// Build the lookup map for initialized categories
axList[i]._categoriesMap = {};
for(j = 0; j < axList[i]._categories.length; j++) {
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
}
if(axList[i].type === 'category') hasCategoryAxis = true;
}
// If traces were specified and this trace was not included,
// then transfer it over from the old calcdata:
for(i = 0; i < fullData.length; i++) {
if(Array.isArray(traces) && traces.indexOf(i) === -1) {
calcdata[i] = oldCalcdata[i];
continue;
}
}
var hasCalcTransform = false;
// transform loop
for(i = 0; i < fullData.length; i++) {
trace = fullData[i];
if(trace.visible === true && trace.transforms) {
_module = trace._module;
// we need one round of trace module calc before
// the calc transform to 'fill in' the categories list
// used for example in the data-to-coordinate method
if(_module && _module.calc) _module.calc(gd, trace);
for(j = 0; j < trace.transforms.length; j++) {
var transform = trace.transforms[j];
_module = transformsRegistry[transform.type];
if(_module && _module.calcTransform) {
hasCalcTransform = true;
_module.calcTransform(gd, trace, transform);
}
}
}
}
// clear stuff that should recomputed in 'regular' loop
if(hasCalcTransform) {
for(i = 0; i < axList.length; i++) {
axList[i]._min = [];
axList[i]._max = [];
axList[i]._categories = [];
// Reset the look up map
axList[i]._categoriesMap = {};
}
}
// 'regular' loop
for(i = 0; i < fullData.length; i++) {
var cd = [];
trace = fullData[i];
if(trace.visible === true) {
_module = trace._module;
if(_module && _module.calc) cd = _module.calc(gd, trace);
}
// Make sure there is a first point.
//
// This ensures there is a calcdata item for every trace,
// even if cartesian logic doesn't handle it (for things like legends).
if(!Array.isArray(cd) || !cd[0]) {
cd = [{x: BADNUM, y: BADNUM}];
}
// add the trace-wide properties to the first point,
// per point properties to every point
// t is the holder for trace-wide properties
if(!cd[0].t) cd[0].t = {};
cd[0].trace = trace;
calcdata[i] = cd;
}
// To handle the case of components using category names as coordinates, we
// need to re-supply defaults for these objects now, after calc has
// finished populating the category mappings
// Any component that uses `Axes.coercePosition` falls into this category
if(hasCategoryAxis) {
var dataReferencedComponents = ['annotations', 'shapes', 'images'];
for(i = 0; i < dataReferencedComponents.length; i++) {
Registry.getComponentMethod(dataReferencedComponents[i], 'supplyLayoutDefaults')(
gd.layout, fullLayout, fullData);
}
}
};
plots.rehover = function(gd) {
if(gd._fullLayout._rehover) {
gd._fullLayout._rehover();
}
};
plots.generalUpdatePerTraceModule = function(subplot, subplotCalcData, subplotLayout) {
var traceHashOld = subplot.traceHash,
traceHash = {},
i;
function filterVisible(calcDataIn) {
var calcDataOut = [];
for(var i = 0; i < calcDataIn.length; i++) {
var calcTrace = calcDataIn[i],
trace = calcTrace[0].trace;
if(trace.visible === true) calcDataOut.push(calcTrace);
}
return calcDataOut;
}
// build up moduleName -> calcData hash
for(i = 0; i < subplotCalcData.length; i++) {
var calcTraces = subplotCalcData[i],
trace = calcTraces[0].trace;
// skip over visible === false traces
// as they don't have `_module` ref
if(trace.visible) {
traceHash[trace.type] = traceHash[trace.type] || [];
traceHash[trace.type].push(calcTraces);
}
}
var moduleNamesOld = Object.keys(traceHashOld);
var moduleNames = Object.keys(traceHash);
// when a trace gets deleted, make sure that its module's
// plot method is called so that it is properly
// removed from the DOM.
for(i = 0; i < moduleNamesOld.length; i++) {
var moduleName = moduleNamesOld[i];
if(moduleNames.indexOf(moduleName) === -1) {
var fakeCalcTrace = traceHashOld[moduleName][0],
fakeTrace = fakeCalcTrace[0].trace;
fakeTrace.visible = false;
traceHash[moduleName] = [fakeCalcTrace];
}
}
// update list of module names to include 'fake' traces added above
moduleNames = Object.keys(traceHash);
// call module plot method
for(i = 0; i < moduleNames.length; i++) {
var moduleCalcData = traceHash[moduleNames[i]],
_module = moduleCalcData[0][0].trace._module;
_module.plot(subplot, filterVisible(moduleCalcData), subplotLayout);
}
// update moduleName -> calcData hash
subplot.traceHash = traceHash;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var Plots = require('./plots');
/**
* Find and supply defaults to all subplots of a given type
* This handles subplots that are contained within one container - so
* gl3d, geo, ternary... but not 2d axes which have separate x and y axes
* finds subplots, coerces their `domain` attributes, then calls the
* given handleDefaults function to fill in everything else.
*
* layoutIn: the complete user-supplied input layout
* layoutOut: the complete finished layout
* fullData: the finished data array, used only to find subplots
* opts: {
* type: subplot type string
* attributes: subplot attributes object
* partition: 'x' or 'y', which direction to divide domain space by default
* (default 'x', ie side-by-side subplots)
* TODO: this option is only here because 3D and geo made opposite
* choices in this regard previously and I didn't want to change it.
* Instead we should do:
* - something consistent
* - something more square (4 cuts 2x2, 5/6 cuts 2x3, etc.)
* - something that includes all subplot types in one arrangement,
* now that we can have them together!
* handleDefaults: function of (subplotLayoutIn, subplotLayoutOut, coerce, opts)
* this opts object is passed through to handleDefaults, so attach any
* additional items needed by this function here as well
* }
*/
module.exports = function handleSubplotDefaults(layoutIn, layoutOut, fullData, opts) {
var subplotType = opts.type,
subplotAttributes = opts.attributes,
handleDefaults = opts.handleDefaults,
partition = opts.partition || 'x';
var ids = Plots.findSubplotIds(fullData, subplotType),
idsLength = ids.length;
var subplotLayoutIn, subplotLayoutOut;
function coerce(attr, dflt) {
return Lib.coerce(subplotLayoutIn, subplotLayoutOut, subplotAttributes, attr, dflt);
}
for(var i = 0; i < idsLength; i++) {
var id = ids[i];
// ternary traces get a layout ternary for free!
if(layoutIn[id]) subplotLayoutIn = layoutIn[id];
else subplotLayoutIn = layoutIn[id] = {};
layoutOut[id] = subplotLayoutOut = {};
coerce('domain.' + partition, [i / idsLength, (i + 1) / idsLength]);
coerce('domain.' + {x: 'y', y: 'x'}[partition]);
opts.id = id;
handleDefaults(subplotLayoutIn, subplotLayoutOut, coerce, opts);
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| axes.js | 8.18% | (84 / 1027) | 0% | (0 / 958) | 0% | (0 / 85) | 9.08% | (84 / 925) | |
| axis_autotype.js | 19.44% | (7 / 36) | 0% | (0 / 21) | 0% | (0 / 4) | 25.93% | (7 / 27) | |
| axis_defaults.js | 25% | (13 / 52) | 0% | (0 / 32) | 0% | (0 / 2) | 26.53% | (13 / 49) | |
| axis_ids.js | 18.84% | (13 / 69) | 0% | (0 / 40) | 0% | (0 / 10) | 23.21% | (13 / 56) | |
| category_order_defaults.js | 8.33% | (1 / 12) | 0% | (0 / 12) | 0% | (0 / 1) | 11.11% | (1 / 9) | |
| constants.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| constraint_defaults.js | 8.62% | (5 / 58) | 0% | (0 / 28) | 0% | (0 / 3) | 9.43% | (5 / 53) | |
| constraints.js | 12.5% | (4 / 32) | 0% | (0 / 6) | 0% | (0 / 1) | 12.9% | (4 / 31) | |
| dragbox.js | 10.85% | (55 / 507) | 0% | (0 / 356) | 0% | (0 / 40) | 12.22% | (55 / 450) | |
| graph_interact.js | 6.84% | (43 / 629) | 0% | (0 / 508) | 0% | (0 / 45) | 7.56% | (43 / 569) | |
| index.js | 13.94% | (23 / 165) | 0% | (0 / 68) | 0% | (0 / 13) | 14.11% | (23 / 163) | |
| layout_attributes.js | 100% | (6 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (6 / 6) | |
| layout_defaults.js | 13.68% | (16 / 117) | 0% | (0 / 75) | 0% | (0 / 5) | 14.16% | (16 / 113) | |
| ordered_categories.js | 12% | (3 / 25) | 0% | (0 / 15) | 0% | (0 / 3) | 13.64% | (3 / 22) | |
| position_defaults.js | 17.65% | (3 / 17) | 0% | (0 / 20) | 0% | (0 / 1) | 20% | (3 / 15) | |
| scale_zoom.js | 14.29% | (1 / 7) | 0% | (0 / 2) | 0% | (0 / 1) | 16.67% | (1 / 6) | |
| select.js | 13.41% | (11 / 82) | 0% | (0 / 30) | 0% | (0 / 8) | 14.67% | (11 / 75) | |
| set_convert.js | 10.73% | (22 / 205) | 0% | (0 / 118) | 0% | (0 / 29) | 12.43% | (22 / 177) | |
| tick_label_defaults.js | 10.34% | (3 / 29) | 0% | (0 / 24) | 0% | (0 / 4) | 11.54% | (3 / 26) | |
| tick_mark_defaults.js | 37.5% | (3 / 8) | 0% | (0 / 8) | 0% | (0 / 1) | 37.5% | (3 / 8) | |
| tick_value_defaults.js | 11.11% | (4 / 36) | 0% | (0 / 47) | 0% | (0 / 1) | 12.12% | (4 / 33) | |
| transition_axes.js | 9.76% | (16 / 164) | 0% | (0 / 58) | 0% | (0 / 13) | 9.94% | (16 / 161) | |
| type_defaults.js | 14.81% | (8 / 54) | 0% | (0 / 55) | 0% | (0 / 5) | 16.67% | (8 / 48) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
xaxis: {
valType: 'subplotid',
role: 'info',
dflt: 'x',
description: [
'Sets a reference between this trace\'s x coordinates and',
'a 2D cartesian x axis.',
'If *x* (the default value), the x coordinates refer to',
'`layout.xaxis`.',
'If *x2*, the x coordinates refer to `layout.xaxis2`, and so on.'
].join(' ')
},
yaxis: {
valType: 'subplotid',
role: 'info',
dflt: 'y',
description: [
'Sets a reference between this trace\'s y coordinates and',
'a 2D cartesian y axis.',
'If *y* (the default value), the y coordinates refer to',
'`layout.yaxis`.',
'If *y2*, the y coordinates refer to `layout.xaxis2`, and so on.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var Titles = require('../../components/titles');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var constants = require('../../constants/numerical');
var FP_SAFE = constants.FP_SAFE;
var ONEAVGYEAR = constants.ONEAVGYEAR;
var ONEAVGMONTH = constants.ONEAVGMONTH;
var ONEDAY = constants.ONEDAY;
var ONEHOUR = constants.ONEHOUR;
var ONEMIN = constants.ONEMIN;
var ONESEC = constants.ONESEC;
var BADNUM = constants.BADNUM;
var axes = module.exports = {};
axes.layoutAttributes = require('./layout_attributes');
axes.supplyLayoutDefaults = require('./layout_defaults');
axes.setConvert = require('./set_convert');
var axisIds = require('./axis_ids');
axes.id2name = axisIds.id2name;
axes.cleanId = axisIds.cleanId;
axes.list = axisIds.list;
axes.listIds = axisIds.listIds;
axes.getFromId = axisIds.getFromId;
axes.getFromTrace = axisIds.getFromTrace;
/*
* find the list of possible axes to reference with an xref or yref attribute
* and coerce it to that list
*
* attr: the attribute we're generating a reference for. Should end in 'x' or 'y'
* but can be prefixed, like 'ax' for annotation's arrow x
* dflt: the default to coerce to, or blank to use the first axis (falling back on
* extraOption if there is no axis)
* extraOption: aside from existing axes with this letter, what non-axis value is allowed?
* Only required if it's different from `dflt`
*/
axes.coerceRef = function(containerIn, containerOut, gd, attr, dflt, extraOption) {
var axLetter = attr.charAt(attr.length - 1),
axlist = axes.listIds(gd, axLetter),
refAttr = attr + 'ref',
attrDef = {};
if(!dflt) dflt = axlist[0] || extraOption;
if(!extraOption) extraOption = dflt;
// data-ref annotations are not supported in gl2d yet
attrDef[refAttr] = {
valType: 'enumerated',
values: axlist.concat(extraOption ? [extraOption] : []),
dflt: dflt
};
// xref, yref
return Lib.coerce(containerIn, containerOut, attrDef, refAttr);
};
/*
* coerce position attributes (range-type) that can be either on axes or absolute
* (paper or pixel) referenced. The biggest complication here is that we don't know
* before looking at the axis whether the value must be a number or not (it may be
* a date string), so we can't use the regular valType='number' machinery
*
* axRef (string): the axis this position is referenced to, or:
* paper: fraction of the plot area
* pixel: pixels relative to some starting position
* attr (string): the attribute in containerOut we are coercing
* dflt (number): the default position, as a fraction or pixels. If the attribute
* is to be axis-referenced, this will be converted to an axis data value
*
* Also cleans the values, since the attribute definition itself has to say
* valType: 'any' to handle date axes. This allows us to accept:
* - for category axes: category names, and convert them here into serial numbers.
* Note that this will NOT work for axis range endpoints, because we don't know
* the category list yet (it's set by ax.makeCalcdata during calc)
* but it works for component (note, shape, images) positions.
* - for date axes: JS Dates or milliseconds, and convert to date strings
* - for other types: coerce them to numbers
*/
axes.coercePosition = function(containerOut, gd, coerce, axRef, attr, dflt) {
var pos,
newPos;
if(axRef === 'paper' || axRef === 'pixel') {
pos = coerce(attr, dflt);
}
else {
var ax = axes.getFromId(gd, axRef);
dflt = ax.fraction2r(dflt);
pos = coerce(attr, dflt);
if(ax.type === 'category') {
// if position is given as a category name, convert it to a number
if(typeof pos === 'string' && (ax._categories || []).length) {
newPos = ax._categories.indexOf(pos);
containerOut[attr] = (newPos === -1) ? dflt : newPos;
return;
}
}
else if(ax.type === 'date') {
containerOut[attr] = Lib.cleanDate(pos, BADNUM, ax.calendar);
return;
}
}
// finally make sure we have a number (unless date type already returned a string)
containerOut[attr] = isNumeric(pos) ? Number(pos) : dflt;
};
// empty out types for all axes containing these traces
// so we auto-set them again
axes.clearTypes = function(gd, traces) {
if(!Array.isArray(traces) || !traces.length) {
traces = (gd._fullData).map(function(d, i) { return i; });
}
traces.forEach(function(tracenum) {
var trace = gd.data[tracenum];
delete (axes.getFromId(gd, trace.xaxis) || {}).type;
delete (axes.getFromId(gd, trace.yaxis) || {}).type;
});
};
// get counteraxis letter for this axis (name or id)
// this can also be used as the id for default counter axis
axes.counterLetter = function(id) {
var axLetter = id.charAt(0);
if(axLetter === 'x') return 'y';
if(axLetter === 'y') return 'x';
};
// incorporate a new minimum difference and first tick into
// forced
// note that _forceTick0 is linearized, so needs to be turned into
// a range value for setting tick0
axes.minDtick = function(ax, newDiff, newFirst, allow) {
// doesn't make sense to do forced min dTick on log or category axes,
// and the plot itself may decide to cancel (ie non-grouped bars)
if(['log', 'category'].indexOf(ax.type) !== -1 || !allow) {
ax._minDtick = 0;
}
// undefined means there's nothing there yet
else if(ax._minDtick === undefined) {
ax._minDtick = newDiff;
ax._forceTick0 = newFirst;
}
else if(ax._minDtick) {
// existing minDtick is an integer multiple of newDiff
// (within rounding err)
// and forceTick0 can be shifted to newFirst
if((ax._minDtick / newDiff + 1e-6) % 1 < 2e-6 &&
(((newFirst - ax._forceTick0) / newDiff % 1) +
1.000001) % 1 < 2e-6) {
ax._minDtick = newDiff;
ax._forceTick0 = newFirst;
}
// if the converse is true (newDiff is a multiple of minDtick and
// newFirst can be shifted to forceTick0) then do nothing - same
// forcing stands. Otherwise, cancel forced minimum
else if((newDiff / ax._minDtick + 1e-6) % 1 > 2e-6 ||
(((newFirst - ax._forceTick0) / ax._minDtick % 1) +
1.000001) % 1 > 2e-6) {
ax._minDtick = 0;
}
}
};
// Find the autorange for this axis
//
// assumes ax._min and ax._max have already been set by calling axes.expand
// using calcdata from all traces. These are arrays of:
// {val: calcdata value, pad: extra pixels beyond this value}
//
// Returns an array of [min, max]. These are calcdata for log and category axes
// and data for linear and date axes.
//
// TODO: we want to change log to data as well, but it's hard to do this
// maintaining backward compatibility. category will always have to use calcdata
// though, because otherwise values between categories (or outside all categories)
// would be impossible.
axes.getAutoRange = function(ax) {
var newRange = [];
var minmin = ax._min[0].val,
maxmax = ax._max[0].val,
i;
for(i = 1; i < ax._min.length; i++) {
if(minmin !== maxmax) break;
minmin = Math.min(minmin, ax._min[i].val);
}
for(i = 1; i < ax._max.length; i++) {
if(minmin !== maxmax) break;
maxmax = Math.max(maxmax, ax._max[i].val);
}
var j, minpt, maxpt, minbest, maxbest, dp, dv,
mbest = 0,
axReverse = false;
if(ax.range) {
var rng = Lib.simpleMap(ax.range, ax.r2l);
axReverse = rng[1] < rng[0];
}
// one-time setting to easily reverse the axis
// when plotting from code
if(ax.autorange === 'reversed') {
axReverse = true;
ax.autorange = true;
}
for(i = 0; i < ax._min.length; i++) {
minpt = ax._min[i];
for(j = 0; j < ax._max.length; j++) {
maxpt = ax._max[j];
dv = maxpt.val - minpt.val;
dp = ax._length - minpt.pad - maxpt.pad;
if(dv > 0 && dp > 0 && dv / dp > mbest) {
minbest = minpt;
maxbest = maxpt;
mbest = dv / dp;
}
}
}
if(minmin === maxmax) {
var lower = minmin - 1;
var upper = minmin + 1;
if(ax.rangemode === 'tozero') {
newRange = minmin < 0 ? [lower, 0] : [0, upper];
}
else if(ax.rangemode === 'nonnegative') {
newRange = [Math.max(0, lower), Math.max(0, upper)];
}
else {
newRange = [lower, upper];
}
}
else if(mbest) {
if(ax.type === 'linear' || ax.type === '-') {
if(ax.rangemode === 'tozero') {
if(minbest.val >= 0) {
minbest = {val: 0, pad: 0};
}
if(maxbest.val <= 0) {
maxbest = {val: 0, pad: 0};
}
}
else if(ax.rangemode === 'nonnegative') {
if(minbest.val - mbest * minbest.pad < 0) {
minbest = {val: 0, pad: 0};
}
if(maxbest.val < 0) {
maxbest = {val: 1, pad: 0};
}
}
// in case it changed again...
mbest = (maxbest.val - minbest.val) /
(ax._length - minbest.pad - maxbest.pad);
}
newRange = [
minbest.val - mbest * minbest.pad,
maxbest.val + mbest * maxbest.pad
];
}
// don't let axis have zero size, while still respecting tozero and nonnegative
if(newRange[0] === newRange[1]) {
if(ax.rangemode === 'tozero') {
if(newRange[0] < 0) {
newRange = [newRange[0], 0];
}
else if(newRange[0] > 0) {
newRange = [0, newRange[0]];
}
else {
newRange = [0, 1];
}
}
else {
newRange = [newRange[0] - 1, newRange[0] + 1];
if(ax.rangemode === 'nonnegative') {
newRange[0] = Math.max(0, newRange[0]);
}
}
}
// maintain reversal
if(axReverse) newRange.reverse();
return Lib.simpleMap(newRange, ax.l2r || Number);
};
axes.doAutoRange = function(ax) {
if(!ax._length) ax.setScale();
// TODO do we really need this?
var hasDeps = (ax._min && ax._max && ax._min.length && ax._max.length);
if(ax.autorange && hasDeps) {
ax.range = axes.getAutoRange(ax);
// doAutoRange will get called on fullLayout,
// but we want to report its results back to layout
var axIn = ax._input;
axIn.range = ax.range.slice();
axIn.autorange = ax.autorange;
}
};
// save a copy of the initial axis ranges in fullLayout
// use them in mode bar and dblclick events
axes.saveRangeInitial = function(gd, overwrite) {
var axList = axes.list(gd, '', true),
hasOneAxisChanged = false;
for(var i = 0; i < axList.length; i++) {
var ax = axList[i];
var isNew = (ax._rangeInitial === undefined);
var hasChanged = (
isNew || !(
ax.range[0] === ax._rangeInitial[0] &&
ax.range[1] === ax._rangeInitial[1]
)
);
if((isNew && ax.autorange === false) || (overwrite && hasChanged)) {
ax._rangeInitial = ax.range.slice();
hasOneAxisChanged = true;
}
}
return hasOneAxisChanged;
};
// save a copy of the initial spike visibility
axes.saveShowSpikeInitial = function(gd, overwrite) {
var axList = axes.list(gd, '', true),
hasOneAxisChanged = false,
allEnabled = 'on';
for(var i = 0; i < axList.length; i++) {
var ax = axList[i];
var isNew = (ax._showSpikeInitial === undefined);
var hasChanged = (
isNew || !(
ax.showspikes === ax._showspikes
)
);
if((isNew) || (overwrite && hasChanged)) {
ax._showSpikeInitial = ax.showspikes;
hasOneAxisChanged = true;
}
if(allEnabled === 'on' && !ax.showspikes) {
allEnabled = 'off';
}
}
gd._fullLayout._cartesianSpikesEnabled = allEnabled;
return hasOneAxisChanged;
};
// axes.expand: if autoranging, include new data in the outer limits
// for this axis
// data is an array of numbers (ie already run through ax.d2c)
// available options:
// vpad: (number or number array) pad values (data value +-vpad)
// ppad: (number or number array) pad pixels (pixel location +-ppad)
// ppadplus, ppadminus, vpadplus, vpadminus:
// separate padding for each side, overrides symmetric
// padded: (boolean) add 5% padding to both ends
// (unless one end is overridden by tozero)
// tozero: (boolean) make sure to include zero if axis is linear,
// and make it a tight bound if possible
axes.expand = function(ax, data, options) {
var needsAutorange = (
ax.autorange ||
!!Lib.nestedProperty(ax, 'rangeslider.autorange').get()
);
if(!needsAutorange || !data) return;
if(!ax._min) ax._min = [];
if(!ax._max) ax._max = [];
if(!options) options = {};
if(!ax._m) ax.setScale();
var len = data.length,
extrappad = options.padded ? ax._length * 0.05 : 0,
tozero = options.tozero && (ax.type === 'linear' || ax.type === '-'),
i, j, v, di, dmin, dmax,
ppadiplus, ppadiminus, includeThis, vmin, vmax;
function getPad(item) {
if(Array.isArray(item)) {
return function(i) { return Math.max(Number(item[i]||0), 0); };
}
else {
var v = Math.max(Number(item||0), 0);
return function() { return v; };
}
}
var ppadplus = getPad((ax._m > 0 ?
options.ppadplus : options.ppadminus) || options.ppad || 0),
ppadminus = getPad((ax._m > 0 ?
options.ppadminus : options.ppadplus) || options.ppad || 0),
vpadplus = getPad(options.vpadplus || options.vpad),
vpadminus = getPad(options.vpadminus || options.vpad);
function addItem(i) {
di = data[i];
if(!isNumeric(di)) return;
ppadiplus = ppadplus(i) + extrappad;
ppadiminus = ppadminus(i) + extrappad;
vmin = di - vpadminus(i);
vmax = di + vpadplus(i);
// special case for log axes: if vpad makes this object span
// more than an order of mag, clip it to one order. This is so
// we don't have non-positive errors or absurdly large lower
// range due to rounding errors
if(ax.type === 'log' && vmin < vmax / 10) { vmin = vmax / 10; }
dmin = ax.c2l(vmin);
dmax = ax.c2l(vmax);
if(tozero) {
dmin = Math.min(0, dmin);
dmax = Math.max(0, dmax);
}
// In order to stop overflow errors, don't consider points
// too close to the limits of js floating point
function goodNumber(v) {
return isNumeric(v) && Math.abs(v) < FP_SAFE;
}
if(goodNumber(dmin)) {
includeThis = true;
// take items v from ax._min and compare them to the
// presently active point:
// - if the item supercedes the new point, set includethis false
// - if the new pt supercedes the item, delete it from ax._min
for(j = 0; j < ax._min.length && includeThis; j++) {
v = ax._min[j];
if(v.val <= dmin && v.pad >= ppadiminus) {
includeThis = false;
}
else if(v.val >= dmin && v.pad <= ppadiminus) {
ax._min.splice(j, 1);
j--;
}
}
if(includeThis) {
ax._min.push({
val: dmin,
pad: (tozero && dmin === 0) ? 0 : ppadiminus
});
}
}
if(goodNumber(dmax)) {
includeThis = true;
for(j = 0; j < ax._max.length && includeThis; j++) {
v = ax._max[j];
if(v.val >= dmax && v.pad >= ppadiplus) {
includeThis = false;
}
else if(v.val <= dmax && v.pad <= ppadiplus) {
ax._max.splice(j, 1);
j--;
}
}
if(includeThis) {
ax._max.push({
val: dmax,
pad: (tozero && dmax === 0) ? 0 : ppadiplus
});
}
}
}
// For efficiency covering monotonic or near-monotonic data,
// check a few points at both ends first and then sweep
// through the middle
for(i = 0; i < 6; i++) addItem(i);
for(i = len - 1; i > 5; i--) addItem(i);
};
axes.autoBin = function(data, ax, nbins, is2d, calendar) {
var dataMin = Lib.aggNums(Math.min, null, data),
dataMax = Lib.aggNums(Math.max, null, data);
if(!calendar) calendar = ax.calendar;
if(ax.type === 'category') {
return {
start: dataMin - 0.5,
end: dataMax + 0.5,
size: 1
};
}
var size0;
if(nbins) size0 = ((dataMax - dataMin) / nbins);
else {
// totally auto: scale off std deviation so the highest bin is
// somewhat taller than the total number of bins, but don't let
// the size get smaller than the 'nice' rounded down minimum
// difference between values
var distinctData = Lib.distinctVals(data),
msexp = Math.pow(10, Math.floor(
Math.log(distinctData.minDiff) / Math.LN10)),
minSize = msexp * Lib.roundUp(
distinctData.minDiff / msexp, [0.9, 1.9, 4.9, 9.9], true);
size0 = Math.max(minSize, 2 * Lib.stdev(data) /
Math.pow(data.length, is2d ? 0.25 : 0.4));
// fallback if ax.d2c output BADNUMs
// e.g. when user try to plot categorical bins
// on a layout.xaxis.type: 'linear'
if(!isNumeric(size0)) size0 = 1;
}
// piggyback off autotick code to make "nice" bin sizes
var dummyAx;
if(ax.type === 'log') {
dummyAx = {
type: 'linear',
range: [dataMin, dataMax]
};
}
else {
dummyAx = {
type: ax.type,
range: Lib.simpleMap([dataMin, dataMax], ax.c2r, 0, calendar),
calendar: calendar
};
}
axes.setConvert(dummyAx);
axes.autoTicks(dummyAx, size0);
var binStart = axes.tickIncrement(
axes.tickFirst(dummyAx), dummyAx.dtick, 'reverse', calendar),
binEnd;
// check for too many data points right at the edges of bins
// (>50% within 1% of bin edges) or all data points integral
// and offset the bins accordingly
if(typeof dummyAx.dtick === 'number') {
binStart = autoShiftNumericBins(binStart, data, dummyAx, dataMin, dataMax);
var bincount = 1 + Math.floor((dataMax - binStart) / dummyAx.dtick);
binEnd = binStart + bincount * dummyAx.dtick;
}
else {
// month ticks - should be the only nonlinear kind we have at this point.
// dtick (as supplied by axes.autoTick) only has nonlinear values on
// date and log axes, but even if you display a histogram on a log axis
// we bin it on a linear axis (which one could argue against, but that's
// a separate issue)
if(dummyAx.dtick.charAt(0) === 'M') {
binStart = autoShiftMonthBins(binStart, data, dummyAx.dtick, dataMin, calendar);
}
// calculate the endpoint for nonlinear ticks - you have to
// just increment until you're done
binEnd = binStart;
while(binEnd <= dataMax) {
binEnd = axes.tickIncrement(binEnd, dummyAx.dtick, false, calendar);
}
}
return {
start: ax.c2r(binStart, 0, calendar),
end: ax.c2r(binEnd, 0, calendar),
size: dummyAx.dtick
};
};
function autoShiftNumericBins(binStart, data, ax, dataMin, dataMax) {
var edgecount = 0,
midcount = 0,
intcount = 0,
blankCount = 0;
function nearEdge(v) {
// is a value within 1% of a bin edge?
return (1 + (v - binStart) * 100 / ax.dtick) % 100 < 2;
}
for(var i = 0; i < data.length; i++) {
if(data[i] % 1 === 0) intcount++;
else if(!isNumeric(data[i])) blankCount++;
if(nearEdge(data[i])) edgecount++;
if(nearEdge(data[i] + ax.dtick / 2)) midcount++;
}
var dataCount = data.length - blankCount;
if(intcount === dataCount && ax.type !== 'date') {
// all integers: if bin size is <1, it's because
// that was specifically requested (large nbins)
// so respect that... but center the bins containing
// integers on those integers
if(ax.dtick < 1) {
binStart = dataMin - 0.5 * ax.dtick;
}
// otherwise start half an integer down regardless of
// the bin size, just enough to clear up endpoint
// ambiguity about which integers are in which bins.
else {
binStart -= 0.5;
if(binStart + ax.dtick < dataMin) binStart += ax.dtick;
}
}
else if(midcount < dataCount * 0.1) {
if(edgecount > dataCount * 0.3 ||
nearEdge(dataMin) || nearEdge(dataMax)) {
// lots of points at the edge, not many in the middle
// shift half a bin
var binshift = ax.dtick / 2;
binStart += (binStart + binshift < dataMin) ? binshift : -binshift;
}
}
return binStart;
}
function autoShiftMonthBins(binStart, data, dtick, dataMin, calendar) {
var stats = Lib.findExactDates(data, calendar);
// number of data points that needs to be an exact value
// to shift that increment to (near) the bin center
var threshold = 0.8;
if(stats.exactDays > threshold) {
var numMonths = Number(dtick.substr(1));
if((stats.exactYears > threshold) && (numMonths % 12 === 0)) {
// The exact middle of a non-leap-year is 1.5 days into July
// so if we start the bins here, all but leap years will
// get hover-labeled as exact years.
binStart = axes.tickIncrement(binStart, 'M6', 'reverse') + ONEDAY * 1.5;
}
else if(stats.exactMonths > threshold) {
// Months are not as clean, but if we shift half the *longest*
// month (31/2 days) then 31-day months will get labeled exactly
// and shorter months will get labeled with the correct month
// but shifted 12-36 hours into it.
binStart = axes.tickIncrement(binStart, 'M1', 'reverse') + ONEDAY * 15.5;
}
else {
// Shifting half a day is exact, but since these are month bins it
// will always give a somewhat odd-looking label, until we do something
// smarter like showing the bin boundaries (or the bounds of the actual
// data in each bin)
binStart -= ONEDAY / 2;
}
var nextBinStart = axes.tickIncrement(binStart, dtick);
if(nextBinStart <= dataMin) return nextBinStart;
}
return binStart;
}
// ----------------------------------------------------
// Ticks and grids
// ----------------------------------------------------
// calculate the ticks: text, values, positioning
// if ticks are set to automatic, determine the right values (tick0,dtick)
// in any case, set tickround to # of digits to round tick labels to,
// or codes to this effect for log and date scales
axes.calcTicks = function calcTicks(ax) {
var rng = Lib.simpleMap(ax.range, ax.r2l);
// calculate max number of (auto) ticks to display based on plot size
if(ax.tickmode === 'auto' || !ax.dtick) {
var nt = ax.nticks,
minPx;
if(!nt) {
if(ax.type === 'category') {
minPx = ax.tickfont ? (ax.tickfont.size || 12) * 1.2 : 15;
nt = ax._length / minPx;
}
else {
minPx = ax._id.charAt(0) === 'y' ? 40 : 80;
nt = Lib.constrain(ax._length / minPx, 4, 9) + 1;
}
}
// add a couple of extra digits for filling in ticks when we
// have explicit tickvals without tick text
if(ax.tickmode === 'array') nt *= 100;
axes.autoTicks(ax, Math.abs(rng[1] - rng[0]) / nt);
// check for a forced minimum dtick
if(ax._minDtick > 0 && ax.dtick < ax._minDtick * 2) {
ax.dtick = ax._minDtick;
ax.tick0 = ax.l2r(ax._forceTick0);
}
}
// check for missing tick0
if(!ax.tick0) {
ax.tick0 = (ax.type === 'date') ? '2000-01-01' : 0;
}
// now figure out rounding of tick values
autoTickRound(ax);
// now that we've figured out the auto values for formatting
// in case we're missing some ticktext, we can break out for array ticks
if(ax.tickmode === 'array') return arrayTicks(ax);
// find the first tick
ax._tmin = axes.tickFirst(ax);
// check for reversed axis
var axrev = (rng[1] < rng[0]);
// return the full set of tick vals
var vals = [],
// add a tiny bit so we get ticks which may have rounded out
endtick = rng[1] * 1.0001 - rng[0] * 0.0001;
if(ax.type === 'category') {
endtick = (axrev) ? Math.max(-0.5, endtick) :
Math.min(ax._categories.length - 0.5, endtick);
}
for(var x = ax._tmin;
(axrev) ? (x >= endtick) : (x <= endtick);
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)) {
vals.push(x);
// prevent infinite loops
if(vals.length > 1000) break;
}
// save the last tick as well as first, so we can
// show the exponent only on the last one
ax._tmax = vals[vals.length - 1];
// for showing the rest of a date when the main tick label is only the
// latter part: ax._prevDateHead holds what we showed most recently.
// Start with it cleared and mark that we're in calcTicks (ie calculating a
// whole string of these so we should care what the previous date head was!)
ax._prevDateHead = '';
ax._inCalcTicks = true;
var ticksOut = new Array(vals.length);
for(var i = 0; i < vals.length; i++) ticksOut[i] = axes.tickText(ax, vals[i]);
ax._inCalcTicks = false;
return ticksOut;
};
function arrayTicks(ax) {
var vals = ax.tickvals,
text = ax.ticktext,
ticksOut = new Array(vals.length),
rng = Lib.simpleMap(ax.range, ax.r2l),
r0expanded = rng[0] * 1.0001 - rng[1] * 0.0001,
r1expanded = rng[1] * 1.0001 - rng[0] * 0.0001,
tickMin = Math.min(r0expanded, r1expanded),
tickMax = Math.max(r0expanded, r1expanded),
vali,
i,
j = 0;
// without a text array, just format the given values as any other ticks
// except with more precision to the numbers
if(!Array.isArray(text)) text = [];
// make sure showing ticks doesn't accidentally add new categories
var tickVal2l = ax.type === 'category' ? ax.d2l_noadd : ax.d2l;
// array ticks on log axes always show the full number
// (if no explicit ticktext overrides it)
if(ax.type === 'log' && String(ax.dtick).charAt(0) !== 'L') {
ax.dtick = 'L' + Math.pow(10, Math.floor(Math.min(ax.range[0], ax.range[1])) - 1);
}
for(i = 0; i < vals.length; i++) {
vali = tickVal2l(vals[i]);
if(vali > tickMin && vali < tickMax) {
if(text[i] === undefined) ticksOut[j] = axes.tickText(ax, vali);
else ticksOut[j] = tickTextObj(ax, vali, String(text[i]));
j++;
}
}
if(j < vals.length) ticksOut.splice(j, vals.length - j);
return ticksOut;
}
var roundBase10 = [2, 5, 10],
roundBase24 = [1, 2, 3, 6, 12],
roundBase60 = [1, 2, 5, 10, 15, 30],
// 2&3 day ticks are weird, but need something btwn 1&7
roundDays = [1, 2, 3, 7, 14],
// approx. tick positions for log axes, showing all (1) and just 1, 2, 5 (2)
// these don't have to be exact, just close enough to round to the right value
roundLog1 = [-0.046, 0, 0.301, 0.477, 0.602, 0.699, 0.778, 0.845, 0.903, 0.954, 1],
roundLog2 = [-0.301, 0, 0.301, 0.699, 1];
function roundDTick(roughDTick, base, roundingSet) {
return base * Lib.roundUp(roughDTick / base, roundingSet);
}
// autoTicks: calculate best guess at pleasant ticks for this axis
// inputs:
// ax - an axis object
// roughDTick - rough tick spacing (to be turned into a nice round number)
// outputs (into ax):
// tick0: starting point for ticks (not necessarily on the graph)
// usually 0 for numeric (=10^0=1 for log) or jan 1, 2000 for dates
// dtick: the actual, nice round tick spacing, usually a little larger than roughDTick
// if the ticks are spaced linearly (linear scale, categories,
// log with only full powers, date ticks < month),
// this will just be a number
// months: M#
// years: M# where # is 12*number of years
// log with linear ticks: L# where # is the linear tick spacing
// log showing powers plus some intermediates:
// D1 shows all digits, D2 shows 2 and 5
axes.autoTicks = function(ax, roughDTick) {
var base;
if(ax.type === 'date') {
ax.tick0 = Lib.dateTick0(ax.calendar);
// the criteria below are all based on the rough spacing we calculate
// being > half of the final unit - so precalculate twice the rough val
var roughX2 = 2 * roughDTick;
if(roughX2 > ONEAVGYEAR) {
roughDTick /= ONEAVGYEAR;
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
ax.dtick = 'M' + (12 * roundDTick(roughDTick, base, roundBase10));
}
else if(roughX2 > ONEAVGMONTH) {
roughDTick /= ONEAVGMONTH;
ax.dtick = 'M' + roundDTick(roughDTick, 1, roundBase24);
}
else if(roughX2 > ONEDAY) {
ax.dtick = roundDTick(roughDTick, ONEDAY, roundDays);
// get week ticks on sunday
// this will also move the base tick off 2000-01-01 if dtick is
// 2 or 3 days... but that's a weird enough case that we'll ignore it.
ax.tick0 = Lib.dateTick0(ax.calendar, true);
}
else if(roughX2 > ONEHOUR) {
ax.dtick = roundDTick(roughDTick, ONEHOUR, roundBase24);
}
else if(roughX2 > ONEMIN) {
ax.dtick = roundDTick(roughDTick, ONEMIN, roundBase60);
}
else if(roughX2 > ONESEC) {
ax.dtick = roundDTick(roughDTick, ONESEC, roundBase60);
}
else {
// milliseconds
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
ax.dtick = roundDTick(roughDTick, base, roundBase10);
}
}
else if(ax.type === 'log') {
ax.tick0 = 0;
var rng = Lib.simpleMap(ax.range, ax.r2l);
if(roughDTick > 0.7) {
// only show powers of 10
ax.dtick = Math.ceil(roughDTick);
}
else if(Math.abs(rng[1] - rng[0]) < 1) {
// span is less than one power of 10
var nt = 1.5 * Math.abs((rng[1] - rng[0]) / roughDTick);
// ticks on a linear scale, labeled fully
roughDTick = Math.abs(Math.pow(10, rng[1]) -
Math.pow(10, rng[0])) / nt;
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
ax.dtick = 'L' + roundDTick(roughDTick, base, roundBase10);
}
else {
// include intermediates between powers of 10,
// labeled with small digits
// ax.dtick = "D2" (show 2 and 5) or "D1" (show all digits)
ax.dtick = (roughDTick > 0.3) ? 'D2' : 'D1';
}
}
else if(ax.type === 'category') {
ax.tick0 = 0;
ax.dtick = Math.ceil(Math.max(roughDTick, 1));
}
else {
// auto ticks always start at 0
ax.tick0 = 0;
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
ax.dtick = roundDTick(roughDTick, base, roundBase10);
}
// prevent infinite loops
if(ax.dtick === 0) ax.dtick = 1;
// TODO: this is from log axis histograms with autorange off
if(!isNumeric(ax.dtick) && typeof ax.dtick !== 'string') {
var olddtick = ax.dtick;
ax.dtick = 1;
throw 'ax.dtick error: ' + String(olddtick);
}
};
// after dtick is already known, find tickround = precision
// to display in tick labels
// for numeric ticks, integer # digits after . to round to
// for date ticks, the last date part to show (y,m,d,H,M,S)
// or an integer # digits past seconds
function autoTickRound(ax) {
var dtick = ax.dtick;
ax._tickexponent = 0;
if(!isNumeric(dtick) && typeof dtick !== 'string') {
dtick = 1;
}
if(ax.type === 'category') {
ax._tickround = null;
}
if(ax.type === 'date') {
// If tick0 is unusual, give tickround a bit more information
// not necessarily *all* the information in tick0 though, if it's really odd
// minimal string length for tick0: 'd' is 10, 'M' is 16, 'S' is 19
// take off a leading minus (year < 0) and i (intercalary month) so length is consistent
var tick0ms = ax.r2l(ax.tick0),
tick0str = ax.l2r(tick0ms).replace(/(^-|i)/g, ''),
tick0len = tick0str.length;
if(String(dtick).charAt(0) === 'M') {
// any tick0 more specific than a year: alway show the full date
if(tick0len > 10 || tick0str.substr(5) !== '01-01') ax._tickround = 'd';
// show the month unless ticks are full multiples of a year
else ax._tickround = (+(dtick.substr(1)) % 12 === 0) ? 'y' : 'm';
}
else if((dtick >= ONEDAY && tick0len <= 10) || (dtick >= ONEDAY * 15)) ax._tickround = 'd';
else if((dtick >= ONEMIN && tick0len <= 16) || (dtick >= ONEHOUR)) ax._tickround = 'M';
else if((dtick >= ONESEC && tick0len <= 19) || (dtick >= ONEMIN)) ax._tickround = 'S';
else {
// tickround is a number of digits of fractional seconds
// of any two adjacent ticks, at least one will have the maximum fractional digits
// of all possible ticks - so take the max. length of tick0 and the next one
var tick1len = ax.l2r(tick0ms + dtick).replace(/^-/, '').length;
ax._tickround = Math.max(tick0len, tick1len) - 20;
}
}
else if(isNumeric(dtick) || dtick.charAt(0) === 'L') {
// linear or log (except D1, D2)
var rng = ax.range.map(ax.r2d || Number);
if(!isNumeric(dtick)) dtick = Number(dtick.substr(1));
// 2 digits past largest digit of dtick
ax._tickround = 2 - Math.floor(Math.log(dtick) / Math.LN10 + 0.01);
var maxend = Math.max(Math.abs(rng[0]), Math.abs(rng[1]));
var rangeexp = Math.floor(Math.log(maxend) / Math.LN10 + 0.01);
if(Math.abs(rangeexp) > 3) {
if(ax.exponentformat === 'SI' || ax.exponentformat === 'B') {
ax._tickexponent = 3 * Math.round((rangeexp - 1) / 3);
}
else ax._tickexponent = rangeexp;
}
}
// D1 or D2 (log)
else ax._tickround = null;
}
// months and years don't have constant millisecond values
// (but a year is always 12 months so we only need months)
// log-scale ticks are also not consistently spaced, except
// for pure powers of 10
// numeric ticks always have constant differences, other datetime ticks
// can all be calculated as constant number of milliseconds
axes.tickIncrement = function(x, dtick, axrev, calendar) {
var axSign = axrev ? -1 : 1;
// includes linear, all dates smaller than month, and pure 10^n in log
if(isNumeric(dtick)) return x + axSign * dtick;
// everything else is a string, one character plus a number
var tType = dtick.charAt(0),
dtSigned = axSign * Number(dtick.substr(1));
// Dates: months (or years - see Lib.incrementMonth)
if(tType === 'M') return Lib.incrementMonth(x, dtSigned, calendar);
// Log scales: Linear, Digits
else if(tType === 'L') return Math.log(Math.pow(10, x) + dtSigned) / Math.LN10;
// log10 of 2,5,10, or all digits (logs just have to be
// close enough to round)
else if(tType === 'D') {
var tickset = (dtick === 'D2') ? roundLog2 : roundLog1,
x2 = x + axSign * 0.01,
frac = Lib.roundUp(Lib.mod(x2, 1), tickset, axrev);
return Math.floor(x2) +
Math.log(d3.round(Math.pow(10, frac), 1)) / Math.LN10;
}
else throw 'unrecognized dtick ' + String(dtick);
};
// calculate the first tick on an axis
axes.tickFirst = function(ax) {
var r2l = ax.r2l || Number,
rng = Lib.simpleMap(ax.range, r2l),
axrev = rng[1] < rng[0],
sRound = axrev ? Math.floor : Math.ceil,
// add a tiny extra bit to make sure we get ticks
// that may have been rounded out
r0 = rng[0] * 1.0001 - rng[1] * 0.0001,
dtick = ax.dtick,
tick0 = r2l(ax.tick0);
if(isNumeric(dtick)) {
var tmin = sRound((r0 - tick0) / dtick) * dtick + tick0;
// make sure no ticks outside the category list
if(ax.type === 'category') {
tmin = Lib.constrain(tmin, 0, ax._categories.length - 1);
}
return tmin;
}
var tType = dtick.charAt(0),
dtNum = Number(dtick.substr(1));
// Dates: months (or years)
if(tType === 'M') {
var cnt = 0,
t0 = tick0,
t1,
mult,
newDTick;
// This algorithm should work for *any* nonlinear (but close to linear!)
// tick spacing. Limit to 10 iterations, for gregorian months it's normally <=3.
while(cnt < 10) {
t1 = axes.tickIncrement(t0, dtick, axrev, ax.calendar);
if((t1 - r0) * (t0 - r0) <= 0) {
// t1 and t0 are on opposite sides of r0! we've succeeded!
if(axrev) return Math.min(t0, t1);
return Math.max(t0, t1);
}
mult = (r0 - ((t0 + t1) / 2)) / (t1 - t0);
newDTick = tType + ((Math.abs(Math.round(mult)) || 1) * dtNum);
t0 = axes.tickIncrement(t0, newDTick, mult < 0 ? !axrev : axrev, ax.calendar);
cnt++;
}
Lib.error('tickFirst did not converge', ax);
return t0;
}
// Log scales: Linear, Digits
else if(tType === 'L') {
return Math.log(sRound(
(Math.pow(10, r0) - tick0) / dtNum) * dtNum + tick0) / Math.LN10;
}
else if(tType === 'D') {
var tickset = (dtick === 'D2') ? roundLog2 : roundLog1,
frac = Lib.roundUp(Lib.mod(r0, 1), tickset, axrev);
return Math.floor(r0) +
Math.log(d3.round(Math.pow(10, frac), 1)) / Math.LN10;
}
else throw 'unrecognized dtick ' + String(dtick);
};
// draw the text for one tick.
// px,py are the location on gd.paper
// prefix is there so the x axis ticks can be dropped a line
// ax is the axis layout, x is the tick value
// hover is a (truthy) flag for whether to show numbers with a bit
// more precision for hovertext
axes.tickText = function(ax, x, hover) {
var out = tickTextObj(ax, x),
hideexp,
arrayMode = ax.tickmode === 'array',
extraPrecision = hover || arrayMode,
i,
tickVal2l = ax.type === 'category' ? ax.d2l_noadd : ax.d2l;
if(arrayMode && Array.isArray(ax.ticktext)) {
var rng = Lib.simpleMap(ax.range, ax.r2l),
minDiff = Math.abs(rng[1] - rng[0]) / 10000;
for(i = 0; i < ax.ticktext.length; i++) {
if(Math.abs(x - tickVal2l(ax.tickvals[i])) < minDiff) break;
}
if(i < ax.ticktext.length) {
out.text = String(ax.ticktext[i]);
return out;
}
}
function isHidden(showAttr) {
var first_or_last;
if(showAttr === undefined) return true;
if(hover) return showAttr === 'none';
first_or_last = {
first: ax._tmin,
last: ax._tmax
}[showAttr];
return showAttr !== 'all' && x !== first_or_last;
}
hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : '';
if(ax.type === 'date') formatDate(ax, out, hover, extraPrecision);
else if(ax.type === 'log') formatLog(ax, out, hover, extraPrecision, hideexp);
else if(ax.type === 'category') formatCategory(ax, out);
else formatLinear(ax, out, hover, extraPrecision, hideexp);
// add prefix and suffix
if(ax.tickprefix && !isHidden(ax.showtickprefix)) out.text = ax.tickprefix + out.text;
if(ax.ticksuffix && !isHidden(ax.showticksuffix)) out.text += ax.ticksuffix;
return out;
};
function tickTextObj(ax, x, text) {
var tf = ax.tickfont || {};
return {
x: x,
dx: 0,
dy: 0,
text: text || '',
fontSize: tf.size,
font: tf.family,
fontColor: tf.color
};
}
function formatDate(ax, out, hover, extraPrecision) {
var tr = ax._tickround,
fmt = (hover && ax.hoverformat) || ax.tickformat;
if(extraPrecision) {
// second or sub-second precision: extra always shows max digits.
// for other fields, extra precision just adds one field.
if(isNumeric(tr)) tr = 4;
else tr = {y: 'm', m: 'd', d: 'M', M: 'S', S: 4}[tr];
}
var dateStr = Lib.formatDate(out.x, fmt, tr, ax.calendar),
headStr;
var splitIndex = dateStr.indexOf('\n');
if(splitIndex !== -1) {
headStr = dateStr.substr(splitIndex + 1);
dateStr = dateStr.substr(0, splitIndex);
}
if(extraPrecision) {
// if extraPrecision led to trailing zeros, strip them off
// actually, this can lead to removing even more zeros than
// in the original rounding, but that's fine because in these
// contexts uniformity is not so important (if there's even
// anything to be uniform with!)
// can we remove the whole time part?
if(dateStr === '00:00:00' || dateStr === '00:00') {
dateStr = headStr;
headStr = '';
}
else if(dateStr.length === 8) {
// strip off seconds if they're zero (zero fractional seconds
// are already omitted)
// but we never remove minutes and leave just hours
dateStr = dateStr.replace(/:00$/, '');
}
}
if(headStr) {
if(hover) {
// hover puts it all on one line, so headPart works best up front
// except for year headPart: turn this into "Jan 1, 2000" etc.
if(tr === 'd') dateStr += ', ' + headStr;
else dateStr = headStr + (dateStr ? ', ' + dateStr : '');
}
else if(!ax._inCalcTicks || (headStr !== ax._prevDateHead)) {
dateStr += '<br>' + headStr;
ax._prevDateHead = headStr;
}
}
out.text = dateStr;
}
function formatLog(ax, out, hover, extraPrecision, hideexp) {
var dtick = ax.dtick,
x = out.x;
if(extraPrecision && ((typeof dtick !== 'string') || dtick.charAt(0) !== 'L')) dtick = 'L3';
if(ax.tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
out.text = numFormat(Math.pow(10, x), ax, hideexp, extraPrecision);
}
else if(isNumeric(dtick) || ((dtick.charAt(0) === 'D') && (Lib.mod(x + 0.01, 1) < 0.1))) {
if(['e', 'E', 'power'].indexOf(ax.exponentformat) !== -1) {
var p = Math.round(x);
if(p === 0) out.text = 1;
else if(p === 1) out.text = '10';
else if(p > 1) out.text = '10<sup>' + p + '</sup>';
else out.text = '10<sup>\u2212' + -p + '</sup>';
out.fontSize *= 1.25;
}
else {
out.text = numFormat(Math.pow(10, x), ax, '', 'fakehover');
if(dtick === 'D1' && ax._id.charAt(0) === 'y') {
out.dy -= out.fontSize / 6;
}
}
}
else if(dtick.charAt(0) === 'D') {
out.text = String(Math.round(Math.pow(10, Lib.mod(x, 1))));
out.fontSize *= 0.75;
}
else throw 'unrecognized dtick ' + String(dtick);
// if 9's are printed on log scale, move the 10's away a bit
if(ax.dtick === 'D1') {
var firstChar = String(out.text).charAt(0);
if(firstChar === '0' || firstChar === '1') {
if(ax._id.charAt(0) === 'y') {
out.dx -= out.fontSize / 4;
}
else {
out.dy += out.fontSize / 2;
out.dx += (ax.range[1] > ax.range[0] ? 1 : -1) *
out.fontSize * (x < 0 ? 0.5 : 0.25);
}
}
}
}
function formatCategory(ax, out) {
var tt = ax._categories[Math.round(out.x)];
if(tt === undefined) tt = '';
out.text = String(tt);
}
function formatLinear(ax, out, hover, extraPrecision, hideexp) {
// don't add an exponent to zero if we're showing all exponents
// so the only reason you'd show an exponent on zero is if it's the
// ONLY tick to get an exponent (first or last)
if(ax.showexponent === 'all' && Math.abs(out.x / ax.dtick) < 1e-6) {
hideexp = 'hide';
}
out.text = numFormat(out.x, ax, hideexp, extraPrecision);
}
// format a number (tick value) according to the axis settings
// new, more reliable procedure than d3.round or similar:
// add half the rounding increment, then stringify and truncate
// also automatically switch to sci. notation
var SIPREFIXES = ['f', 'p', 'n', 'μ', 'm', '', 'k', 'M', 'G', 'T'];
function numFormat(v, ax, fmtoverride, hover) {
// negative?
var isNeg = v < 0,
// max number of digits past decimal point to show
tickRound = ax._tickround,
exponentFormat = fmtoverride || ax.exponentformat || 'B',
exponent = ax._tickexponent,
tickformat = ax.tickformat,
separatethousands = ax.separatethousands;
// special case for hover: set exponent just for this value, and
// add a couple more digits of precision over tick labels
if(hover) {
// make a dummy axis obj to get the auto rounding and exponent
var ah = {
exponentformat: ax.exponentformat,
dtick: ax.showexponent === 'none' ? ax.dtick :
(isNumeric(v) ? Math.abs(v) || 1 : 1),
// if not showing any exponents, don't change the exponent
// from what we calculate
range: ax.showexponent === 'none' ? ax.range.map(ax.r2d) : [0, v || 1]
};
autoTickRound(ah);
tickRound = (Number(ah._tickround) || 0) + 4;
exponent = ah._tickexponent;
if(ax.hoverformat) tickformat = ax.hoverformat;
}
if(tickformat) return d3.format(tickformat)(v).replace(/-/g, '\u2212');
// 'epsilon' - rounding increment
var e = Math.pow(10, -tickRound) / 2;
// exponentFormat codes:
// 'e' (1.2e+6, default)
// 'E' (1.2E+6)
// 'SI' (1.2M)
// 'B' (same as SI except 10^9=B not G)
// 'none' (1200000)
// 'power' (1.2x10^6)
// 'hide' (1.2, use 3rd argument=='hide' to eg
// only show exponent on last tick)
if(exponentFormat === 'none') exponent = 0;
// take the sign out, put it back manually at the end
// - makes cases easier
v = Math.abs(v);
if(v < e) {
// 0 is just 0, but may get exponent if it's the last tick
v = '0';
isNeg = false;
}
else {
v += e;
// take out a common exponent, if any
if(exponent) {
v *= Math.pow(10, -exponent);
tickRound += exponent;
}
// round the mantissa
if(tickRound === 0) v = String(Math.floor(v));
else if(tickRound < 0) {
v = String(Math.round(v));
v = v.substr(0, v.length + tickRound);
for(var i = tickRound; i < 0; i++) v += '0';
}
else {
v = String(v);
var dp = v.indexOf('.') + 1;
if(dp) v = v.substr(0, dp + tickRound).replace(/\.?0+$/, '');
}
// insert appropriate decimal point and thousands separator
v = Lib.numSeparate(v, ax._separators, separatethousands);
}
// add exponent
if(exponent && exponentFormat !== 'hide') {
var signedExponent;
if(exponent < 0) signedExponent = '\u2212' + -exponent;
else if(exponentFormat !== 'power') signedExponent = '+' + exponent;
else signedExponent = String(exponent);
if(exponentFormat === 'e' ||
((exponentFormat === 'SI' || exponentFormat === 'B') &&
(exponent > 12 || exponent < -15))) {
v += 'e' + signedExponent;
}
else if(exponentFormat === 'E') {
v += 'E' + signedExponent;
}
else if(exponentFormat === 'power') {
v += '×10<sup>' + signedExponent + '</sup>';
}
else if(exponentFormat === 'B' && exponent === 9) {
v += 'B';
}
else if(exponentFormat === 'SI' || exponentFormat === 'B') {
v += SIPREFIXES[exponent / 3 + 5];
}
}
// put sign back in and return
// replace standard minus character (which is technically a hyphen)
// with a true minus sign
if(isNeg) return '\u2212' + v;
return v;
}
axes.subplotMatch = /^x([0-9]*)y([0-9]*)$/;
// getSubplots - extract all combinations of axes we need to make plots for
// as an array of items like 'xy', 'x2y', 'x2y2'...
// sorted by x (x,x2,x3...) then y
// optionally restrict to only subplots containing axis object ax
// looks both for combinations of x and y found in the data
// and at axes and their anchors
axes.getSubplots = function(gd, ax) {
var subplots = [];
var i, j, sp;
// look for subplots in the data
var data = gd._fullData || gd.data || [];
for(i = 0; i < data.length; i++) {
var trace = data[i];
if(trace.visible === false || trace.visible === 'legendonly' ||
!(Registry.traceIs(trace, 'cartesian') || Registry.traceIs(trace, 'gl2d'))
) continue;
var xId = trace.xaxis || 'x',
yId = trace.yaxis || 'y';
sp = xId + yId;
if(subplots.indexOf(sp) === -1) subplots.push(sp);
}
// look for subplots in the axes/anchors, so that we at least draw all axes
var axesList = axes.list(gd, '', true);
function hasAx2(sp, ax2) {
return sp.indexOf(ax2._id) !== -1;
}
for(i = 0; i < axesList.length; i++) {
var ax2 = axesList[i],
ax2Letter = ax2._id.charAt(0),
ax3Id = (ax2.anchor === 'free') ?
((ax2Letter === 'x') ? 'y' : 'x') :
ax2.anchor,
ax3 = axes.getFromId(gd, ax3Id);
// look if ax2 is already represented in the data
var foundAx2 = false;
for(j = 0; j < subplots.length; j++) {
if(hasAx2(subplots[j], ax2)) {
foundAx2 = true;
break;
}
}
// ignore free axes that already represented in the data
if(ax2.anchor === 'free' && foundAx2) continue;
// ignore anchor-less axes
if(!ax3) continue;
sp = (ax2Letter === 'x') ?
ax2._id + ax3._id :
ax3._id + ax2._id;
if(subplots.indexOf(sp) === -1) subplots.push(sp);
}
// filter invalid subplots
var spMatch = axes.subplotMatch,
allSubplots = [];
for(i = 0; i < subplots.length; i++) {
sp = subplots[i];
if(spMatch.test(sp)) allSubplots.push(sp);
}
// sort the subplot ids
allSubplots.sort(function(a, b) {
var aMatch = a.match(spMatch),
bMatch = b.match(spMatch);
if(aMatch[1] === bMatch[1]) {
return +(aMatch[2] || 1) - (bMatch[2] || 1);
}
return +(aMatch[1]||0) - (bMatch[1]||0);
});
if(ax) return axes.findSubplotsWithAxis(allSubplots, ax);
return allSubplots;
};
// find all subplots with axis 'ax'
axes.findSubplotsWithAxis = function(subplots, ax) {
var axMatch = new RegExp(
(ax._id.charAt(0) === 'x') ? ('^' + ax._id + 'y') : (ax._id + '$')
);
var subplotsWithAxis = [];
for(var i = 0; i < subplots.length; i++) {
var sp = subplots[i];
if(axMatch.test(sp)) subplotsWithAxis.push(sp);
}
return subplotsWithAxis;
};
// makeClipPaths: prepare clipPaths for all single axes and all possible xy pairings
axes.makeClipPaths = function(gd) {
var fullLayout = gd._fullLayout,
defs = fullLayout._defs,
fullWidth = {_offset: 0, _length: fullLayout.width, _id: ''},
fullHeight = {_offset: 0, _length: fullLayout.height, _id: ''},
xaList = axes.list(gd, 'x', true),
yaList = axes.list(gd, 'y', true),
clipList = [],
i,
j;
for(i = 0; i < xaList.length; i++) {
clipList.push({x: xaList[i], y: fullHeight});
for(j = 0; j < yaList.length; j++) {
if(i === 0) clipList.push({x: fullWidth, y: yaList[j]});
clipList.push({x: xaList[i], y: yaList[j]});
}
}
var defGroup = defs.selectAll('g.clips')
.data([0]);
defGroup.enter().append('g')
.classed('clips', true);
// selectors don't work right with camelCase tags,
// have to use class instead
// https://groups.google.com/forum/#!topic/d3-js/6EpAzQ2gU9I
var axClips = defGroup.selectAll('.axesclip')
.data(clipList, function(d) { return d.x._id + d.y._id; });
axClips.enter().append('clipPath')
.classed('axesclip', true)
.attr('id', function(d) { return 'clip' + fullLayout._uid + d.x._id + d.y._id; })
.append('rect');
axClips.exit().remove();
axClips.each(function(d) {
d3.select(this).select('rect').attr({
x: d.x._offset || 0,
y: d.y._offset || 0,
width: d.x._length || 1,
height: d.y._length || 1
});
});
};
// doTicks: draw ticks, grids, and tick labels
// axid: 'x', 'y', 'x2' etc,
// blank to do all,
// 'redraw' to force full redraw, and reset:
// ax._r (stored range for use by zoom/pan)
// ax._rl (stored linearized range for use by zoom/pan)
// or can pass in an axis object directly
axes.doTicks = function(gd, axid, skipTitle) {
var fullLayout = gd._fullLayout,
ax,
independent = false;
// allow passing an independent axis object instead of id
if(typeof axid === 'object') {
ax = axid;
axid = ax._id;
independent = true;
}
else {
ax = axes.getFromId(gd, axid);
if(axid === 'redraw') {
fullLayout._paper.selectAll('g.subplot').each(function(subplot) {
var plotinfo = fullLayout._plots[subplot],
xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
plotinfo.xaxislayer
.selectAll('.' + xa._id + 'tick').remove();
plotinfo.yaxislayer
.selectAll('.' + ya._id + 'tick').remove();
plotinfo.gridlayer
.selectAll('path').remove();
plotinfo.zerolinelayer
.selectAll('path').remove();
});
}
if(!axid || axid === 'redraw') {
return Lib.syncOrAsync(axes.list(gd, '', true).map(function(ax) {
return function() {
if(!ax._id) return;
var axDone = axes.doTicks(gd, ax._id);
if(axid === 'redraw') {
ax._r = ax.range.slice();
ax._rl = Lib.simpleMap(ax._r, ax.r2l);
}
return axDone;
};
}));
}
}
// make sure we only have allowed options for exponents
// (others can make confusing errors)
if(!ax.tickformat) {
if(['none', 'e', 'E', 'power', 'SI', 'B'].indexOf(ax.exponentformat) === -1) {
ax.exponentformat = 'e';
}
if(['all', 'first', 'last', 'none'].indexOf(ax.showexponent) === -1) {
ax.showexponent = 'all';
}
}
// set scaling to pixels
ax.setScale();
var axLetter = axid.charAt(0),
counterLetter = axes.counterLetter(axid),
vals = axes.calcTicks(ax),
datafn = function(d) { return [d.text, d.x, ax.mirror].join('_'); },
tcls = axid + 'tick',
gcls = axid + 'grid',
zcls = axid + 'zl',
pad = (ax.linewidth || 1) / 2,
labelStandoff =
(ax.ticks === 'outside' ? ax.ticklen : 1) + (ax.linewidth || 0),
labelShift = 0,
gridWidth = Drawing.crispRound(gd, ax.gridwidth, 1),
zeroLineWidth = Drawing.crispRound(gd, ax.zerolinewidth, gridWidth),
tickWidth = Drawing.crispRound(gd, ax.tickwidth, 1),
sides, transfn, tickpathfn, subplots,
i;
if(ax._counterangle && ax.ticks === 'outside') {
var caRad = ax._counterangle * Math.PI / 180;
labelStandoff = ax.ticklen * Math.cos(caRad) + (ax.linewidth || 0);
labelShift = ax.ticklen * Math.sin(caRad);
}
// positioning arguments for x vs y axes
if(axLetter === 'x') {
sides = ['bottom', 'top'];
transfn = function(d) {
return 'translate(' + ax.l2p(d.x) + ',0)';
};
tickpathfn = function(shift, len) {
if(ax._counterangle) {
var caRad = ax._counterangle * Math.PI / 180;
return 'M0,' + shift + 'l' + (Math.sin(caRad) * len) + ',' + (Math.cos(caRad) * len);
}
else return 'M0,' + shift + 'v' + len;
};
}
else if(axLetter === 'y') {
sides = ['left', 'right'];
transfn = function(d) {
return 'translate(0,' + ax.l2p(d.x) + ')';
};
tickpathfn = function(shift, len) {
if(ax._counterangle) {
var caRad = ax._counterangle * Math.PI / 180;
return 'M' + shift + ',0l' + (Math.cos(caRad) * len) + ',' + (-Math.sin(caRad) * len);
}
else return 'M' + shift + ',0h' + len;
};
}
else {
Lib.warn('Unrecognized doTicks axis:', axid);
return;
}
var axside = ax.side || sides[0],
// which direction do the side[0], side[1], and free ticks go?
// then we flip if outside XOR y axis
ticksign = [-1, 1, axside === sides[1] ? 1 : -1];
if((ax.ticks !== 'inside') === (axLetter === 'x')) {
ticksign = ticksign.map(function(v) { return -v; });
}
if(!ax.visible) return;
// remove zero lines, grid lines, and inside ticks if they're within
// 1 pixel of the end
// The key case here is removing zero lines when the axis bound is zero.
function clipEnds(d) {
var p = ax.l2p(d.x);
return (p > 1 && p < ax._length - 1);
}
var valsClipped = vals.filter(clipEnds);
function drawTicks(container, tickpath) {
var ticks = container.selectAll('path.' + tcls)
.data(ax.ticks === 'inside' ? valsClipped : vals, datafn);
if(tickpath && ax.ticks) {
ticks.enter().append('path').classed(tcls, 1).classed('ticks', 1)
.classed('crisp', 1)
.call(Color.stroke, ax.tickcolor)
.style('stroke-width', tickWidth + 'px')
.attr('d', tickpath);
ticks.attr('transform', transfn);
ticks.exit().remove();
}
else ticks.remove();
}
function drawLabels(container, position) {
// tick labels - for now just the main labels.
// TODO: mirror labels, esp for subplots
var tickLabels = container.selectAll('g.' + tcls).data(vals, datafn);
if(!ax.showticklabels || !isNumeric(position)) {
tickLabels.remove();
drawAxTitle();
return;
}
var labelx, labely, labelanchor, labelpos0, flipit;
if(axLetter === 'x') {
flipit = (axside === 'bottom') ? 1 : -1;
labelx = function(d) { return d.dx + labelShift * flipit; };
labelpos0 = position + (labelStandoff + pad) * flipit;
labely = function(d) {
return d.dy + labelpos0 + d.fontSize *
((axside === 'bottom') ? 1 : -0.5);
};
labelanchor = function(angle) {
if(!isNumeric(angle) || angle === 0 || angle === 180) {
return 'middle';
}
return (angle * flipit < 0) ? 'end' : 'start';
};
}
else {
flipit = (axside === 'right') ? 1 : -1;
labely = function(d) { return d.dy + d.fontSize / 2 - labelShift * flipit; };
labelx = function(d) {
return d.dx + position + (labelStandoff + pad +
((Math.abs(ax.tickangle) === 90) ? d.fontSize / 2 : 0)) * flipit;
};
labelanchor = function(angle) {
if(isNumeric(angle) && Math.abs(angle) === 90) {
return 'middle';
}
return axside === 'right' ? 'start' : 'end';
};
}
var maxFontSize = 0,
autoangle = 0,
labelsReady = [];
tickLabels.enter().append('g').classed(tcls, 1)
.append('text')
// only so tex has predictable alignment that we can
// alter later
.attr('text-anchor', 'middle')
.each(function(d) {
var thisLabel = d3.select(this),
newPromise = gd._promises.length;
thisLabel
.call(Drawing.setPosition, labelx(d), labely(d))
.call(Drawing.font, d.font, d.fontSize, d.fontColor)
.text(d.text)
.call(svgTextUtils.convertToTspans);
newPromise = gd._promises[newPromise];
if(newPromise) {
// if we have an async label, we'll deal with that
// all here so take it out of gd._promises and
// instead position the label and promise this in
// labelsReady
labelsReady.push(gd._promises.pop().then(function() {
positionLabels(thisLabel, ax.tickangle);
}));
}
else {
// sync label: just position it now.
positionLabels(thisLabel, ax.tickangle);
}
});
tickLabels.exit().remove();
tickLabels.each(function(d) {
maxFontSize = Math.max(maxFontSize, d.fontSize);
});
function positionLabels(s, angle) {
s.each(function(d) {
var anchor = labelanchor(angle);
var thisLabel = d3.select(this),
mathjaxGroup = thisLabel.select('.text-math-group'),
transform = transfn(d) +
((isNumeric(angle) && +angle !== 0) ?
(' rotate(' + angle + ',' + labelx(d) + ',' +
(labely(d) - d.fontSize / 2) + ')') :
'');
if(mathjaxGroup.empty()) {
var txt = thisLabel.select('text').attr({
transform: transform,
'text-anchor': anchor
});
if(!txt.empty()) {
txt.selectAll('tspan.line').attr({
x: txt.attr('x'),
y: txt.attr('y')
});
}
}
else {
var mjShift =
Drawing.bBox(mathjaxGroup.node()).width *
{end: -0.5, start: 0.5}[anchor];
mathjaxGroup.attr('transform', transform +
(mjShift ? 'translate(' + mjShift + ',0)' : ''));
}
});
}
// make sure all labels are correctly positioned at their base angle
// the positionLabels call above is only for newly drawn labels.
// do this without waiting, using the last calculated angle to
// minimize flicker, then do it again when we know all labels are
// there, putting back the prescribed angle to check for overlaps.
positionLabels(tickLabels, ax._lastangle || ax.tickangle);
function allLabelsReady() {
return labelsReady.length && Promise.all(labelsReady);
}
function fixLabelOverlaps() {
positionLabels(tickLabels, ax.tickangle);
// check for auto-angling if x labels overlap
// don't auto-angle at all for log axes with
// base and digit format
if(axLetter === 'x' && !isNumeric(ax.tickangle) &&
(ax.type !== 'log' || String(ax.dtick).charAt(0) !== 'D')) {
var lbbArray = [];
tickLabels.each(function(d) {
var s = d3.select(this),
thisLabel = s.select('.text-math-group'),
x = ax.l2p(d.x);
if(thisLabel.empty()) thisLabel = s.select('text');
var bb = Drawing.bBox(thisLabel.node());
lbbArray.push({
// ignore about y, just deal with x overlaps
top: 0,
bottom: 10,
height: 10,
left: x - bb.width / 2,
// impose a 2px gap
right: x + bb.width / 2 + 2,
width: bb.width + 2
});
});
for(i = 0; i < lbbArray.length - 1; i++) {
if(Lib.bBoxIntersect(lbbArray[i], lbbArray[i + 1])) {
// any overlap at all - set 30 degrees
autoangle = 30;
break;
}
}
if(autoangle) {
var tickspacing = Math.abs(
(vals[vals.length - 1].x - vals[0].x) * ax._m
) / (vals.length - 1);
if(tickspacing < maxFontSize * 2.5) {
autoangle = 90;
}
positionLabels(tickLabels, autoangle);
}
ax._lastangle = autoangle;
}
// update the axis title
// (so it can move out of the way if needed)
// TODO: separate out scoot so we don't need to do
// a full redraw of the title (mostly relevant for MathJax)
drawAxTitle();
return axid + ' done';
}
function calcBoundingBox() {
var bBox = container.node().getBoundingClientRect();
var gdBB = gd.getBoundingClientRect();
/*
* the way we're going to use this, the positioning that matters
* is relative to the origin of gd. This is important particularly
* if gd is scrollable, and may have been scrolled between the time
* we calculate this and the time we use it
*/
ax._boundingBox = {
width: bBox.width,
height: bBox.height,
left: bBox.left - gdBB.left,
right: bBox.right - gdBB.left,
top: bBox.top - gdBB.top,
bottom: bBox.bottom - gdBB.top
};
/*
* for spikelines: what's the full domain of positions in the
* opposite direction that are associated with this axis?
* This means any axes that we make a subplot with, plus the
* position of the axis itself if it's free.
*/
if(subplots) {
var fullRange = ax._counterSpan = [Infinity, -Infinity];
for(i = 0; i < subplots.length; i++) {
var subplot = fullLayout._plots[subplots[i]];
var counterAxis = subplot[(axLetter === 'x') ? 'yaxis' : 'xaxis'];
extendRange(fullRange, [
counterAxis._offset,
counterAxis._offset + counterAxis._length
]);
}
if(ax.anchor === 'free') {
extendRange(fullRange, (axLetter === 'x') ?
[ax._boundingBox.bottom, ax._boundingBox.top] :
[ax._boundingBox.right, ax._boundingBox.left]);
}
}
function extendRange(range, newRange) {
range[0] = Math.min(range[0], newRange[0]);
range[1] = Math.max(range[1], newRange[1]);
}
}
var done = Lib.syncOrAsync([
allLabelsReady,
fixLabelOverlaps,
calcBoundingBox
]);
if(done && done.then) gd._promises.push(done);
return done;
}
function drawAxTitle() {
if(skipTitle) return;
// now this only applies to regular cartesian axes; colorbars and
// others ALWAYS call doTicks with skipTitle=true so they can
// configure their own titles.
var ax = axisIds.getFromId(gd, axid),
avoidSelection = d3.select(gd).selectAll('g.' + axid + 'tick'),
avoid = {
selection: avoidSelection,
side: ax.side
},
axLetter = axid.charAt(0),
gs = gd._fullLayout._size,
offsetBase = 1.5,
fontSize = ax.titlefont.size,
transform,
counterAxis,
x,
y;
if(avoidSelection.size()) {
var translation = Drawing.getTranslate(avoidSelection.node().parentNode);
avoid.offsetLeft = translation.x;
avoid.offsetTop = translation.y;
}
if(axLetter === 'x') {
counterAxis = (ax.anchor === 'free') ?
{_offset: gs.t + (1 - (ax.position || 0)) * gs.h, _length: 0} :
axisIds.getFromId(gd, ax.anchor);
x = ax._offset + ax._length / 2;
y = counterAxis._offset + ((ax.side === 'top') ?
-10 - fontSize * (offsetBase + (ax.showticklabels ? 1 : 0)) :
counterAxis._length + 10 +
fontSize * (offsetBase + (ax.showticklabels ? 1.5 : 0.5)));
if(ax.rangeslider && ax.rangeslider.visible && ax._boundingBox) {
y += (fullLayout.height - fullLayout.margin.b - fullLayout.margin.t) *
ax.rangeslider.thickness + ax._boundingBox.height;
}
if(!avoid.side) avoid.side = 'bottom';
}
else {
counterAxis = (ax.anchor === 'free') ?
{_offset: gs.l + (ax.position || 0) * gs.w, _length: 0} :
axisIds.getFromId(gd, ax.anchor);
y = ax._offset + ax._length / 2;
x = counterAxis._offset + ((ax.side === 'right') ?
counterAxis._length + 10 +
fontSize * (offsetBase + (ax.showticklabels ? 1 : 0.5)) :
-10 - fontSize * (offsetBase + (ax.showticklabels ? 0.5 : 0)));
transform = {rotate: '-90', offset: 0};
if(!avoid.side) avoid.side = 'left';
}
Titles.draw(gd, axid + 'title', {
propContainer: ax,
propName: ax._name + '.title',
dfltName: axLetter.toUpperCase() + ' axis',
avoid: avoid,
transform: transform,
attributes: {x: x, y: y, 'text-anchor': 'middle'}
});
}
function traceHasBarsOrFill(trace, subplot) {
if(trace.visible !== true || trace.xaxis + trace.yaxis !== subplot) return false;
if(Registry.traceIs(trace, 'bar') && trace.orientation === {x: 'h', y: 'v'}[axLetter]) return true;
return trace.fill && trace.fill.charAt(trace.fill.length - 1) === axLetter;
}
function drawGrid(plotinfo, counteraxis, subplot) {
var gridcontainer = plotinfo.gridlayer,
zlcontainer = plotinfo.zerolinelayer,
gridvals = plotinfo['hidegrid' + axLetter] ? [] : valsClipped,
gridpath = ax._gridpath ||
'M0,0' + ((axLetter === 'x') ? 'v' : 'h') + counteraxis._length,
grid = gridcontainer.selectAll('path.' + gcls)
.data((ax.showgrid === false) ? [] : gridvals, datafn);
grid.enter().append('path').classed(gcls, 1)
.classed('crisp', 1)
.attr('d', gridpath)
.each(function(d) {
if(ax.zeroline && (ax.type === 'linear' || ax.type === '-') &&
Math.abs(d.x) < ax.dtick / 100) {
d3.select(this).remove();
}
});
grid.attr('transform', transfn)
.call(Color.stroke, ax.gridcolor || '#ddd')
.style('stroke-width', gridWidth + 'px');
grid.exit().remove();
// zero line
if(zlcontainer) {
var hasBarsOrFill = false;
for(var i = 0; i < gd._fullData.length; i++) {
if(traceHasBarsOrFill(gd._fullData[i], subplot)) {
hasBarsOrFill = true;
break;
}
}
var rng = Lib.simpleMap(ax.range, ax.r2l),
showZl = (rng[0] * rng[1] <= 0) && ax.zeroline &&
(ax.type === 'linear' || ax.type === '-') && gridvals.length &&
(hasBarsOrFill || clipEnds({x: 0}) || !ax.showline);
var zl = zlcontainer.selectAll('path.' + zcls)
.data(showZl ? [{x: 0}] : []);
zl.enter().append('path').classed(zcls, 1).classed('zl', 1)
.classed('crisp', 1)
.attr('d', gridpath);
zl.attr('transform', transfn)
.call(Color.stroke, ax.zerolinecolor || Color.defaultLine)
.style('stroke-width', zeroLineWidth + 'px');
zl.exit().remove();
}
}
if(independent) {
drawTicks(ax._axislayer, tickpathfn(ax._pos + pad * ticksign[2], ticksign[2] * ax.ticklen));
if(ax._counteraxis) {
var fictionalPlotinfo = {
gridlayer: ax._gridlayer,
zerolinelayer: ax._zerolinelayer
};
drawGrid(fictionalPlotinfo, ax._counteraxis);
}
return drawLabels(ax._axislayer, ax._pos);
}
else {
subplots = axes.getSubplots(gd, ax);
var alldone = subplots.map(function(subplot) {
var plotinfo = fullLayout._plots[subplot];
if(!fullLayout._has('cartesian')) return;
var container = plotinfo[axLetter + 'axislayer'],
// [bottom or left, top or right, free, main]
linepositions = ax._linepositions[subplot] || [],
counteraxis = plotinfo[counterLetter + 'axis'],
mainSubplot = counteraxis._id === ax.anchor,
ticksides = [false, false, false],
tickpath = '';
// ticks
if(ax.mirror === 'allticks') ticksides = [true, true, false];
else if(mainSubplot) {
if(ax.mirror === 'ticks') ticksides = [true, true, false];
else ticksides[sides.indexOf(axside)] = true;
}
if(ax.mirrors) {
for(i = 0; i < 2; i++) {
var thisMirror = ax.mirrors[counteraxis._id + sides[i]];
if(thisMirror === 'ticks' || thisMirror === 'labels') {
ticksides[i] = true;
}
}
}
// free axis ticks
if(linepositions[2] !== undefined) ticksides[2] = true;
ticksides.forEach(function(showside, sidei) {
var pos = linepositions[sidei],
tsign = ticksign[sidei];
if(showside && isNumeric(pos)) {
tickpath += tickpathfn(pos + pad * tsign, tsign * ax.ticklen);
}
});
drawTicks(container, tickpath);
drawGrid(plotinfo, counteraxis, subplot);
return drawLabels(container, linepositions[3]);
}).filter(function(onedone) { return onedone && onedone.then; });
return alldone.length ? Promise.all(alldone) : 0;
}
};
// swap all the presentation attributes of the axes showing these traces
axes.swap = function(gd, traces) {
var axGroups = makeAxisGroups(gd, traces);
for(var i = 0; i < axGroups.length; i++) {
swapAxisGroup(gd, axGroups[i].x, axGroups[i].y);
}
};
function makeAxisGroups(gd, traces) {
var groups = [],
i,
j;
for(i = 0; i < traces.length; i++) {
var groupsi = [],
xi = gd._fullData[traces[i]].xaxis,
yi = gd._fullData[traces[i]].yaxis;
if(!xi || !yi) continue; // not a 2D cartesian trace?
for(j = 0; j < groups.length; j++) {
if(groups[j].x.indexOf(xi) !== -1 || groups[j].y.indexOf(yi) !== -1) {
groupsi.push(j);
}
}
if(!groupsi.length) {
groups.push({x: [xi], y: [yi]});
continue;
}
var group0 = groups[groupsi[0]],
groupj;
if(groupsi.length > 1) {
for(j = 1; j < groupsi.length; j++) {
groupj = groups[groupsi[j]];
mergeAxisGroups(group0.x, groupj.x);
mergeAxisGroups(group0.y, groupj.y);
}
}
mergeAxisGroups(group0.x, [xi]);
mergeAxisGroups(group0.y, [yi]);
}
return groups;
}
function mergeAxisGroups(intoSet, fromSet) {
for(var i = 0; i < fromSet.length; i++) {
if(intoSet.indexOf(fromSet[i]) === -1) intoSet.push(fromSet[i]);
}
}
function swapAxisGroup(gd, xIds, yIds) {
var i,
j,
xFullAxes = [],
yFullAxes = [],
layout = gd.layout;
for(i = 0; i < xIds.length; i++) xFullAxes.push(axes.getFromId(gd, xIds[i]));
for(i = 0; i < yIds.length; i++) yFullAxes.push(axes.getFromId(gd, yIds[i]));
var allAxKeys = Object.keys(xFullAxes[0]),
noSwapAttrs = [
'anchor', 'domain', 'overlaying', 'position', 'side', 'tickangle'
],
numericTypes = ['linear', 'log'];
for(i = 0; i < allAxKeys.length; i++) {
var keyi = allAxKeys[i],
xVal = xFullAxes[0][keyi],
yVal = yFullAxes[0][keyi],
allEqual = true,
coerceLinearX = false,
coerceLinearY = false;
if(keyi.charAt(0) === '_' || typeof xVal === 'function' ||
noSwapAttrs.indexOf(keyi) !== -1) {
continue;
}
for(j = 1; j < xFullAxes.length && allEqual; j++) {
var xVali = xFullAxes[j][keyi];
if(keyi === 'type' && numericTypes.indexOf(xVal) !== -1 &&
numericTypes.indexOf(xVali) !== -1 && xVal !== xVali) {
// type is special - if we find a mixture of linear and log,
// coerce them all to linear on flipping
coerceLinearX = true;
}
else if(xVali !== xVal) allEqual = false;
}
for(j = 1; j < yFullAxes.length && allEqual; j++) {
var yVali = yFullAxes[j][keyi];
if(keyi === 'type' && numericTypes.indexOf(yVal) !== -1 &&
numericTypes.indexOf(yVali) !== -1 && yVal !== yVali) {
// type is special - if we find a mixture of linear and log,
// coerce them all to linear on flipping
coerceLinearY = true;
}
else if(yFullAxes[j][keyi] !== yVal) allEqual = false;
}
if(allEqual) {
if(coerceLinearX) layout[xFullAxes[0]._name].type = 'linear';
if(coerceLinearY) layout[yFullAxes[0]._name].type = 'linear';
swapAxisAttrs(layout, keyi, xFullAxes, yFullAxes);
}
}
// now swap x&y for any annotations anchored to these x & y
for(i = 0; i < gd._fullLayout.annotations.length; i++) {
var ann = gd._fullLayout.annotations[i];
if(xIds.indexOf(ann.xref) !== -1 &&
yIds.indexOf(ann.yref) !== -1) {
Lib.swapAttrs(layout.annotations[i], ['?']);
}
}
}
function swapAxisAttrs(layout, key, xFullAxes, yFullAxes) {
// in case the value is the default for either axis,
// look at the first axis in each list and see if
// this key's value is undefined
var np = Lib.nestedProperty,
xVal = np(layout[xFullAxes[0]._name], key).get(),
yVal = np(layout[yFullAxes[0]._name], key).get(),
i;
if(key === 'title') {
// special handling of placeholder titles
if(xVal === 'Click to enter X axis title') {
xVal = 'Click to enter Y axis title';
}
if(yVal === 'Click to enter Y axis title') {
yVal = 'Click to enter X axis title';
}
}
for(i = 0; i < xFullAxes.length; i++) {
np(layout, xFullAxes[i]._name + '.' + key).set(yVal);
}
for(i = 0; i < yFullAxes.length; i++) {
np(layout, yFullAxes[i]._name + '.' + key).set(xVal);
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var BADNUM = require('../../constants/numerical').BADNUM;
module.exports = function autoType(array, calendar) {
if(moreDates(array, calendar)) return 'date';
if(category(array)) return 'category';
if(linearOK(array)) return 'linear';
else return '-';
};
// is there at least one number in array? If not, we should leave
// ax.type empty so it can be autoset later
function linearOK(array) {
if(!array) return false;
for(var i = 0; i < array.length; i++) {
if(isNumeric(array[i])) return true;
}
return false;
}
// does the array a have mostly dates rather than numbers?
// note: some values can be neither (such as blanks, text)
// 2- or 4-digit integers can be both, so require twice as many
// dates as non-dates, to exclude cases with mostly 2 & 4 digit
// numbers and a few dates
function moreDates(a, calendar) {
var dcnt = 0,
ncnt = 0,
// test at most 1000 points, evenly spaced
inc = Math.max(1, (a.length - 1) / 1000),
ai;
for(var i = 0; i < a.length; i += inc) {
ai = a[Math.round(i)];
if(Lib.isDateTime(ai, calendar)) dcnt += 1;
if(isNumeric(ai)) ncnt += 1;
}
return (dcnt > ncnt * 2);
}
// are the (x,y)-values in gd.data mostly text?
// require twice as many categories as numbers
function category(a) {
// test at most 1000 points
var inc = Math.max(1, (a.length - 1) / 1000),
curvenums = 0,
curvecats = 0,
ai;
for(var i = 0; i < a.length; i += inc) {
ai = a[Math.round(i)];
if(Lib.cleanNumber(ai) !== BADNUM) curvenums++;
else if(typeof ai === 'string' && ai !== '' && ai !== 'None') curvecats++;
}
return curvecats > curvenums * 2;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorMix = require('tinycolor2').mix;
var Registry = require('../../registry');
var Lib = require('../../lib');
var lightFraction = require('../../components/color/attributes').lightFraction;
var layoutAttributes = require('./layout_attributes');
var handleTickValueDefaults = require('./tick_value_defaults');
var handleTickMarkDefaults = require('./tick_mark_defaults');
var handleTickLabelDefaults = require('./tick_label_defaults');
var handleCategoryOrderDefaults = require('./category_order_defaults');
var setConvert = require('./set_convert');
var orderedCategories = require('./ordered_categories');
/**
* options: object containing:
*
* letter: 'x' or 'y'
* title: name of the axis (ie 'Colorbar') to go in default title
* font: the default font to inherit
* outerTicks: boolean, should ticks default to outside?
* showGrid: boolean, should gridlines be shown by default?
* noHover: boolean, this axis doesn't support hover effects?
* data: the plot data, used to manage categories
* bgColor: the plot background color, to calculate default gridline colors
*/
module.exports = function handleAxisDefaults(containerIn, containerOut, coerce, options, layoutOut) {
var letter = options.letter,
font = options.font || {},
defaultTitle = 'Click to enter ' +
(options.title || (letter.toUpperCase() + ' axis')) +
' title';
function coerce2(attr, dflt) {
return Lib.coerce2(containerIn, containerOut, layoutAttributes, attr, dflt);
}
var visible = coerce('visible', !options.cheateronly);
var axType = containerOut.type;
if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(containerIn, containerOut, 'calendar', options.calendar);
}
setConvert(containerOut, layoutOut);
var autoRange = coerce('autorange', !containerOut.isValidRange(containerIn.range));
if(autoRange) coerce('rangemode');
coerce('range');
containerOut.cleanRange();
handleCategoryOrderDefaults(containerIn, containerOut, coerce);
containerOut._initialCategories = axType === 'category' ?
orderedCategories(letter, containerOut.categoryorder, containerOut.categoryarray, options.data) :
[];
if(!visible) return containerOut;
var dfltColor = coerce('color');
// if axis.color was provided, use it for fonts too; otherwise,
// inherit from global font color in case that was provided.
var dfltFontColor = (dfltColor === containerIn.color) ? dfltColor : font.color;
coerce('title', defaultTitle);
Lib.coerceFont(coerce, 'titlefont', {
family: font.family,
size: Math.round(font.size * 1.2),
color: dfltFontColor
});
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
handleTickMarkDefaults(containerIn, containerOut, coerce, options);
var lineColor = coerce2('linecolor', dfltColor),
lineWidth = coerce2('linewidth'),
showLine = coerce('showline', !!lineColor || !!lineWidth);
if(!showLine) {
delete containerOut.linecolor;
delete containerOut.linewidth;
}
if(showLine || containerOut.ticks) coerce('mirror');
var gridColor = coerce2('gridcolor', colorMix(dfltColor, options.bgColor, lightFraction).toRgbString()),
gridWidth = coerce2('gridwidth'),
showGridLines = coerce('showgrid', options.showGrid || !!gridColor || !!gridWidth);
if(!showGridLines) {
delete containerOut.gridcolor;
delete containerOut.gridwidth;
}
var zeroLineColor = coerce2('zerolinecolor', dfltColor),
zeroLineWidth = coerce2('zerolinewidth'),
showZeroLine = coerce('zeroline', options.showGrid || !!zeroLineColor || !!zeroLineWidth);
if(!showZeroLine) {
delete containerOut.zerolinecolor;
delete containerOut.zerolinewidth;
}
return containerOut;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Plots = require('../plots');
var Lib = require('../../lib');
var constants = require('./constants');
// convert between axis names (xaxis, xaxis2, etc, elements of gd.layout)
// and axis id's (x, x2, etc). Would probably have ditched 'xaxis'
// completely in favor of just 'x' if it weren't ingrained in the API etc.
exports.id2name = function id2name(id) {
if(typeof id !== 'string' || !id.match(constants.AX_ID_PATTERN)) return;
var axNum = id.substr(1);
if(axNum === '1') axNum = '';
return id.charAt(0) + 'axis' + axNum;
};
exports.name2id = function name2id(name) {
if(!name.match(constants.AX_NAME_PATTERN)) return;
var axNum = name.substr(5);
if(axNum === '1') axNum = '';
return name.charAt(0) + axNum;
};
exports.cleanId = function cleanId(id, axLetter) {
if(!id.match(constants.AX_ID_PATTERN)) return;
if(axLetter && id.charAt(0) !== axLetter) return;
var axNum = id.substr(1).replace(/^0+/, '');
if(axNum === '1') axNum = '';
return id.charAt(0) + axNum;
};
// get all axis object names
// optionally restricted to only x or y or z by string axLetter
// and optionally 2D axes only, not those inside 3D scenes
function listNames(gd, axLetter, only2d) {
var fullLayout = gd._fullLayout;
if(!fullLayout) return [];
function filterAxis(obj, extra) {
var keys = Object.keys(obj),
axMatch = /^[xyz]axis[0-9]*/,
out = [];
for(var i = 0; i < keys.length; i++) {
var k = keys[i];
if(axLetter && k.charAt(0) !== axLetter) continue;
if(axMatch.test(k)) out.push(extra + k);
}
return out.sort();
}
var names = filterAxis(fullLayout, '');
if(only2d) return names;
var sceneIds3D = Plots.getSubplotIds(fullLayout, 'gl3d') || [];
for(var i = 0; i < sceneIds3D.length; i++) {
var sceneId = sceneIds3D[i];
names = names.concat(
filterAxis(fullLayout[sceneId], sceneId + '.')
);
}
return names;
}
// get all axis objects, as restricted in listNames
exports.list = function(gd, axletter, only2d) {
return listNames(gd, axletter, only2d)
.map(function(axName) {
return Lib.nestedProperty(gd._fullLayout, axName).get();
});
};
// get all axis ids, optionally restricted by letter
// this only makes sense for 2d axes
exports.listIds = function(gd, axletter) {
return listNames(gd, axletter, true).map(exports.name2id);
};
// get an axis object from its id 'x','x2' etc
// optionally, id can be a subplot (ie 'x2y3') and type gets x or y from it
exports.getFromId = function(gd, id, type) {
var fullLayout = gd._fullLayout;
if(type === 'x') id = id.replace(/y[0-9]*/, '');
else if(type === 'y') id = id.replace(/x[0-9]*/, '');
return fullLayout[exports.id2name(id)];
};
// get an axis object of specified type from the containing trace
exports.getFromTrace = function(gd, fullTrace, type) {
var fullLayout = gd._fullLayout;
var ax = null;
if(Registry.traceIs(fullTrace, 'gl3d')) {
var scene = fullTrace.scene;
if(scene.substr(0, 5) === 'scene') {
ax = fullLayout[scene][type + 'axis'];
}
}
else {
ax = exports.getFromId(gd, fullTrace[type + 'axis'] || type);
}
return ax;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function handleCategoryOrderDefaults(containerIn, containerOut, coerce) { if(containerOut.type !== 'category') return; var arrayIn = containerIn.categoryarray, orderDefault; var isValidArray = (Array.isArray(arrayIn) && arrayIn.length > 0); // override default 'categoryorder' value when non-empty array is supplied if(isValidArray) orderDefault = 'array'; var order = coerce('categoryorder', orderDefault); // coerce 'categoryarray' only in array order case if(order === 'array') coerce('categoryarray'); // cannot set 'categoryorder' to 'array' with an invalid 'categoryarray' if(!isValidArray && order === 'array') { containerOut.categoryorder = 'trace'; } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
idRegex: {
x: /^x([2-9]|[1-9][0-9]+)?$/,
y: /^y([2-9]|[1-9][0-9]+)?$/
},
attrRegex: {
x: /^xaxis([2-9]|[1-9][0-9]+)?$/,
y: /^yaxis([2-9]|[1-9][0-9]+)?$/
},
// axis match regular expression
xAxisMatch: /^xaxis[0-9]*$/,
yAxisMatch: /^yaxis[0-9]*$/,
// pattern matching axis ids and names
AX_ID_PATTERN: /^[xyz][0-9]*$/,
AX_NAME_PATTERN: /^[xyz]axis[0-9]*$/,
// pixels to move mouse before you stop clamping to starting point
MINDRAG: 8,
// smallest dimension allowed for a select box
MINSELECT: 12,
// smallest dimension allowed for a zoombox
MINZOOM: 20,
// width of axis drag regions
DRAGGERSIZE: 20,
// max pixels away from mouse to allow a point to highlight
MAXDIST: 20,
// hover labels for multiple horizontal bars get tilted by this angle
YANGLE: 60,
// size and display constants for hover text
HOVERARROWSIZE: 6, // pixel size of hover arrows
HOVERTEXTPAD: 3, // pixels padding around text
HOVERFONTSIZE: 13,
HOVERFONT: 'Arial, sans-serif',
// minimum time (msec) between hover calls
HOVERMINTIME: 50,
// max pixels off straight before a lasso select line counts as bent
BENDPX: 1.5,
// delay before a redraw (relayout) after smooth panning and zooming
REDRAWDELAY: 50,
// last resort axis ranges for x and y axes if we have no data
DFLTRANGEX: [-1, 6],
DFLTRANGEY: [-1, 4]
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var id2name = require('./axis_ids').id2name;
module.exports = function handleConstraintDefaults(containerIn, containerOut, coerce, allAxisIds, layoutOut) {
var constraintGroups = layoutOut._axisConstraintGroups;
if(containerOut.fixedrange || !containerIn.scaleanchor) return;
var constraintOpts = getConstraintOpts(constraintGroups, containerOut._id, allAxisIds, layoutOut);
var scaleanchor = Lib.coerce(containerIn, containerOut, {
scaleanchor: {
valType: 'enumerated',
values: constraintOpts.linkableAxes
}
}, 'scaleanchor');
if(scaleanchor) {
var scaleratio = coerce('scaleratio');
// TODO: I suppose I could do attribute.min: Number.MIN_VALUE to avoid zero,
// but that seems hacky. Better way to say "must be a positive number"?
// Of course if you use several super-tiny values you could eventually
// force a product of these to zero and all hell would break loose...
// Likewise with super-huge values.
if(!scaleratio) scaleratio = containerOut.scaleratio = 1;
updateConstraintGroups(constraintGroups, constraintOpts.thisGroup,
containerOut._id, scaleanchor, scaleratio);
}
else if(allAxisIds.indexOf(containerIn.scaleanchor) !== -1) {
Lib.warn('ignored ' + containerOut._name + '.scaleanchor: "' +
containerIn.scaleanchor + '" to avoid either an infinite loop ' +
'and possibly inconsistent scaleratios, or because the target' +
'axis has fixed range.');
}
};
function getConstraintOpts(constraintGroups, thisID, allAxisIds, layoutOut) {
// If this axis is already part of a constraint group, we can't
// scaleanchor any other axis in that group, or we'd make a loop.
// Filter allAxisIds to enforce this, also matching axis types.
var thisType = layoutOut[id2name(thisID)].type;
var i, j, idj, axj;
var linkableAxes = [];
for(j = 0; j < allAxisIds.length; j++) {
idj = allAxisIds[j];
if(idj === thisID) continue;
axj = layoutOut[id2name(idj)];
if(axj.type === thisType && !axj.fixedrange) linkableAxes.push(idj);
}
for(i = 0; i < constraintGroups.length; i++) {
if(constraintGroups[i][thisID]) {
var thisGroup = constraintGroups[i];
var linkableAxesNoLoops = [];
for(j = 0; j < linkableAxes.length; j++) {
idj = linkableAxes[j];
if(!thisGroup[idj]) linkableAxesNoLoops.push(idj);
}
return {linkableAxes: linkableAxesNoLoops, thisGroup: thisGroup};
}
}
return {linkableAxes: linkableAxes, thisGroup: null};
}
/*
* Add this axis to the axis constraint groups, which is the collection
* of axes that are all constrained together on scale.
*
* constraintGroups: a list of objects. each object is
* {axis_id: scale_within_group}, where scale_within_group is
* only important relative to the rest of the group, and defines
* the relative scales between all axes in the group
*
* thisGroup: the group the current axis is already in
* thisID: the id if the current axis
* scaleanchor: the id of the axis to scale it with
* scaleratio: the ratio of this axis to the scaleanchor axis
*/
function updateConstraintGroups(constraintGroups, thisGroup, thisID, scaleanchor, scaleratio) {
var i, j, groupi, keyj, thisGroupIndex;
if(thisGroup === null) {
thisGroup = {};
thisGroup[thisID] = 1;
thisGroupIndex = constraintGroups.length;
constraintGroups.push(thisGroup);
}
else {
thisGroupIndex = constraintGroups.indexOf(thisGroup);
}
var thisGroupKeys = Object.keys(thisGroup);
// we know that this axis isn't in any other groups, but we don't know
// about the scaleanchor axis. If it is, we need to merge the groups.
for(i = 0; i < constraintGroups.length; i++) {
groupi = constraintGroups[i];
if(i !== thisGroupIndex && groupi[scaleanchor]) {
var baseScale = groupi[scaleanchor];
for(j = 0; j < thisGroupKeys.length; j++) {
keyj = thisGroupKeys[j];
groupi[keyj] = baseScale * scaleratio * thisGroup[keyj];
}
constraintGroups.splice(thisGroupIndex, 1);
return;
}
}
// otherwise, we insert the new scaleanchor axis as the base scale (1)
// in its group, and scale the rest of the group to it
if(scaleratio !== 1) {
for(j = 0; j < thisGroupKeys.length; j++) {
thisGroup[thisGroupKeys[j]] *= scaleratio;
}
}
thisGroup[scaleanchor] = 1;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var id2name = require('./axis_ids').id2name;
var scaleZoom = require('./scale_zoom');
var ALMOST_EQUAL = require('../../constants/numerical').ALMOST_EQUAL;
module.exports = function enforceAxisConstraints(gd) {
var fullLayout = gd._fullLayout;
var constraintGroups = fullLayout._axisConstraintGroups;
var i, j, axisID, ax, normScale;
for(i = 0; i < constraintGroups.length; i++) {
var group = constraintGroups[i];
var axisIDs = Object.keys(group);
var minScale = Infinity;
var maxScale = 0;
// mostly matchScale will be the same as minScale
// ie we expand axis ranges to encompass *everything*
// that's currently in any of their ranges, but during
// autorange of a subset of axes we will ignore other
// axes for this purpose.
var matchScale = Infinity;
var normScales = {};
var axes = {};
// find the (normalized) scale of each axis in the group
for(j = 0; j < axisIDs.length; j++) {
axisID = axisIDs[j];
axes[axisID] = ax = fullLayout[id2name(axisID)];
// set axis scale here so we can use _m rather than
// having to calculate it from length and range
ax.setScale();
// abs: inverted scales still satisfy the constraint
normScales[axisID] = normScale = Math.abs(ax._m) / group[axisID];
minScale = Math.min(minScale, normScale);
if(ax._constraintShrinkable) {
// this has served its purpose, so remove it
delete ax._constraintShrinkable;
}
else {
matchScale = Math.min(matchScale, normScale);
}
maxScale = Math.max(maxScale, normScale);
}
// Do we have a constraint mismatch? Give a small buffer for rounding errors
if(minScale > ALMOST_EQUAL * maxScale) continue;
// now increase any ranges we need to until all normalized scales are equal
for(j = 0; j < axisIDs.length; j++) {
axisID = axisIDs[j];
normScale = normScales[axisID];
if(normScale !== matchScale) {
scaleZoom(axes[axisID], normScale / matchScale);
}
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var tinycolor = require('tinycolor2');
var Plotly = require('../../plotly');
var Registry = require('../../registry');
var Lib = require('../../lib');
var svgTextUtils = require('../../lib/svg_text_utils');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var setCursor = require('../../lib/setcursor');
var dragElement = require('../../components/dragelement');
var doTicks = require('./axes').doTicks;
var getFromId = require('./axis_ids').getFromId;
var prepSelect = require('./select');
var scaleZoom = require('./scale_zoom');
var constants = require('./constants');
var MINDRAG = constants.MINDRAG;
var MINZOOM = constants.MINZOOM;
// flag for showing "doubleclick to zoom out" only at the beginning
var SHOWZOOMOUTTIP = true;
// dragBox: create an element to drag one or more axis ends
// inputs:
// plotinfo - which subplot are we making dragboxes on?
// x,y,w,h - left, top, width, height of the box
// ns - how does this drag the vertical axis?
// 'n' - top only
// 's' - bottom only
// 'ns' - top and bottom together, difference unchanged
// ew - same for horizontal axis
module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
// mouseDown stores ms of first mousedown event in the last
// DBLCLICKDELAY ms on the drag bars
// numClicks stores how many mousedowns have been seen
// within DBLCLICKDELAY so we can check for click or doubleclick events
// dragged stores whether a drag has occurred, so we don't have to
// redraw unnecessarily, ie if no move bigger than MINDRAG or MINZOOM px
var fullLayout = gd._fullLayout,
zoomlayer = gd._fullLayout._zoomlayer,
isMainDrag = (ns + ew === 'nsew'),
subplots,
xa,
ya,
xs,
ys,
pw,
ph,
xActive,
yActive,
cursor,
isSubplotConstrained,
xaLinked,
yaLinked;
function recomputeAxisLists() {
xa = [plotinfo.xaxis];
ya = [plotinfo.yaxis];
var xa0 = xa[0];
var ya0 = ya[0];
pw = xa0._length;
ph = ya0._length;
var constraintGroups = fullLayout._axisConstraintGroups;
var xIDs = [xa0._id];
var yIDs = [ya0._id];
// if we're dragging two axes at once, also drag overlays
subplots = [plotinfo].concat((ns && ew) ? plotinfo.overlays : []);
for(var i = 1; i < subplots.length; i++) {
var subplotXa = subplots[i].xaxis,
subplotYa = subplots[i].yaxis;
if(xa.indexOf(subplotXa) === -1) {
xa.push(subplotXa);
xIDs.push(subplotXa._id);
}
if(ya.indexOf(subplotYa) === -1) {
ya.push(subplotYa);
yIDs.push(subplotYa._id);
}
}
xActive = isDirectionActive(xa, ew);
yActive = isDirectionActive(ya, ns);
cursor = getDragCursor(yActive + xActive, fullLayout.dragmode);
xs = xa0._offset;
ys = ya0._offset;
var links = calcLinks(constraintGroups, xIDs, yIDs);
isSubplotConstrained = links.xy;
// finally make the list of axis objects to link
xaLinked = [];
for(var xLinkID in links.x) { xaLinked.push(getFromId(gd, xLinkID)); }
yaLinked = [];
for(var yLinkID in links.y) { yaLinked.push(getFromId(gd, yLinkID)); }
}
recomputeAxisLists();
var dragger = makeDragger(plotinfo, ns + ew + 'drag', cursor, x, y, w, h);
// still need to make the element if the axes are disabled
// but nuke its events (except for maindrag which needs them for hover)
// and stop there
if(!yActive && !xActive && !isSelectOrLasso(fullLayout.dragmode)) {
dragger.onmousedown = null;
dragger.style.pointerEvents = isMainDrag ? 'all' : 'none';
return dragger;
}
var dragOptions = {
element: dragger,
gd: gd,
plotinfo: plotinfo,
doubleclick: doubleClick,
prepFn: function(e, startX, startY) {
var dragModeNow = gd._fullLayout.dragmode;
if(isMainDrag) {
// main dragger handles all drag modes, and changes
// to pan (or to zoom if it already is pan) on shift
if(e.shiftKey) {
if(dragModeNow === 'pan') dragModeNow = 'zoom';
else dragModeNow = 'pan';
}
}
// all other draggers just pan
else dragModeNow = 'pan';
if(dragModeNow === 'lasso') dragOptions.minDrag = 1;
else dragOptions.minDrag = undefined;
if(dragModeNow === 'zoom') {
dragOptions.moveFn = zoomMove;
dragOptions.doneFn = zoomDone;
// zoomMove takes care of the threshold, but we need to
// minimize this so that constrained zoom boxes will flip
// orientation at the right place
dragOptions.minDrag = 1;
zoomPrep(e, startX, startY);
}
else if(dragModeNow === 'pan') {
dragOptions.moveFn = plotDrag;
dragOptions.doneFn = dragDone;
clearSelect(zoomlayer);
}
else if(isSelectOrLasso(dragModeNow)) {
dragOptions.xaxes = xa;
dragOptions.yaxes = ya;
prepSelect(e, startX, startY, dragOptions, dragModeNow);
}
}
};
dragElement.init(dragOptions);
var x0,
y0,
box,
lum,
path0,
dimmed,
zoomMode,
zb,
corners;
function zoomPrep(e, startX, startY) {
var dragBBox = dragger.getBoundingClientRect();
x0 = startX - dragBBox.left;
y0 = startY - dragBBox.top;
box = {l: x0, r: x0, w: 0, t: y0, b: y0, h: 0};
lum = gd._hmpixcount ?
(gd._hmlumcount / gd._hmpixcount) :
tinycolor(gd._fullLayout.plot_bgcolor).getLuminance();
path0 = 'M0,0H' + pw + 'V' + ph + 'H0V0';
dimmed = false;
zoomMode = 'xy';
zb = makeZoombox(zoomlayer, lum, xs, ys, path0);
corners = makeCorners(zoomlayer, xs, ys);
clearSelect(zoomlayer);
}
function zoomMove(dx0, dy0) {
if(gd._transitioningWithDuration) {
return false;
}
var x1 = Math.max(0, Math.min(pw, dx0 + x0)),
y1 = Math.max(0, Math.min(ph, dy0 + y0)),
dx = Math.abs(x1 - x0),
dy = Math.abs(y1 - y0);
box.l = Math.min(x0, x1);
box.r = Math.max(x0, x1);
box.t = Math.min(y0, y1);
box.b = Math.max(y0, y1);
function noZoom() {
zoomMode = '';
box.r = box.l;
box.t = box.b;
corners.attr('d', 'M0,0Z');
}
if(isSubplotConstrained) {
if(dx > MINZOOM || dy > MINZOOM) {
zoomMode = 'xy';
if(dx / pw > dy / ph) {
dy = dx * ph / pw;
if(y0 > y1) box.t = y0 - dy;
else box.b = y0 + dy;
}
else {
dx = dy * pw / ph;
if(x0 > x1) box.l = x0 - dx;
else box.r = x0 + dx;
}
corners.attr('d', xyCorners(box));
}
else {
noZoom();
}
}
// look for small drags in one direction or the other,
// and only drag the other axis
else if(!yActive || dy < Math.min(Math.max(dx * 0.6, MINDRAG), MINZOOM)) {
if(dx < MINDRAG) {
noZoom();
}
else {
box.t = 0;
box.b = ph;
zoomMode = 'x';
corners.attr('d', xCorners(box, y0));
}
}
else if(!xActive || dx < Math.min(dy * 0.6, MINZOOM)) {
box.l = 0;
box.r = pw;
zoomMode = 'y';
corners.attr('d', yCorners(box, x0));
}
else {
zoomMode = 'xy';
corners.attr('d', xyCorners(box));
}
box.w = box.r - box.l;
box.h = box.b - box.t;
updateZoombox(zb, corners, box, path0, dimmed, lum);
dimmed = true;
}
function zoomDone(dragged, numClicks) {
if(Math.min(box.h, box.w) < MINDRAG * 2) {
if(numClicks === 2) doubleClick();
return removeZoombox(gd);
}
// TODO: edit linked axes in zoomAxRanges and in dragTail
if(zoomMode === 'xy' || zoomMode === 'x') zoomAxRanges(xa, box.l / pw, box.r / pw, xaLinked);
if(zoomMode === 'xy' || zoomMode === 'y') zoomAxRanges(ya, (ph - box.b) / ph, (ph - box.t) / ph, yaLinked);
removeZoombox(gd);
dragTail(zoomMode);
if(SHOWZOOMOUTTIP && gd.data && gd._context.showTips) {
Lib.notifier('Double-click to<br>zoom back out', 'long');
SHOWZOOMOUTTIP = false;
}
}
function dragDone(dragged, numClicks) {
var singleEnd = (ns + ew).length === 1;
if(dragged) dragTail();
else if(numClicks === 2 && !singleEnd) doubleClick();
else if(numClicks === 1 && singleEnd) {
var ax = ns ? ya[0] : xa[0],
end = (ns === 's' || ew === 'w') ? 0 : 1,
attrStr = ax._name + '.range[' + end + ']',
initialText = getEndText(ax, end),
hAlign = 'left',
vAlign = 'middle';
if(ax.fixedrange) return;
if(ns) {
vAlign = (ns === 'n') ? 'top' : 'bottom';
if(ax.side === 'right') hAlign = 'right';
}
else if(ew === 'e') hAlign = 'right';
if(gd._context.showAxisRangeEntryBoxes) {
d3.select(dragger)
.call(svgTextUtils.makeEditable, null, {
immediate: true,
background: fullLayout.paper_bgcolor,
text: String(initialText),
fill: ax.tickfont ? ax.tickfont.color : '#444',
horizontalAlign: hAlign,
verticalAlign: vAlign
})
.on('edit', function(text) {
var v = ax.d2r(text);
if(v !== undefined) {
Plotly.relayout(gd, attrStr, v);
}
});
}
}
}
// scroll zoom, on all draggers except corners
var scrollViewBox = [0, 0, pw, ph],
// wait a little after scrolling before redrawing
redrawTimer = null,
REDRAWDELAY = constants.REDRAWDELAY,
mainplot = plotinfo.mainplot ?
fullLayout._plots[plotinfo.mainplot] : plotinfo;
function zoomWheel(e) {
// deactivate mousewheel scrolling on embedded graphs
// devs can override this with layout._enablescrollzoom,
// but _ ensures this setting won't leave their page
if(!gd._context.scrollZoom && !fullLayout._enablescrollzoom) {
return;
}
// If a transition is in progress, then disable any behavior:
if(gd._transitioningWithDuration) {
return Lib.pauseEvent(e);
}
var pc = gd.querySelector('.plotly');
recomputeAxisLists();
// if the plot has scrollbars (more than a tiny excess)
// disable scrollzoom too.
if(pc.scrollHeight - pc.clientHeight > 10 ||
pc.scrollWidth - pc.clientWidth > 10) {
return;
}
clearTimeout(redrawTimer);
var wheelDelta = -e.deltaY;
if(!isFinite(wheelDelta)) wheelDelta = e.wheelDelta / 10;
if(!isFinite(wheelDelta)) {
Lib.log('Did not find wheel motion attributes: ', e);
return;
}
var zoom = Math.exp(-Math.min(Math.max(wheelDelta, -20), 20) / 100),
gbb = mainplot.draglayer.select('.nsewdrag')
.node().getBoundingClientRect(),
xfrac = (e.clientX - gbb.left) / gbb.width,
yfrac = (gbb.bottom - e.clientY) / gbb.height,
i;
function zoomWheelOneAxis(ax, centerFraction, zoom) {
if(ax.fixedrange) return;
var axRange = Lib.simpleMap(ax.range, ax.r2l),
v0 = axRange[0] + (axRange[1] - axRange[0]) * centerFraction;
function doZoom(v) { return ax.l2r(v0 + (v - v0) * zoom); }
ax.range = axRange.map(doZoom);
}
if(ew || isSubplotConstrained) {
// if we're only zooming this axis because of constraints,
// zoom it about the center
if(!ew) xfrac = 0.5;
for(i = 0; i < xa.length; i++) zoomWheelOneAxis(xa[i], xfrac, zoom);
scrollViewBox[2] *= zoom;
scrollViewBox[0] += scrollViewBox[2] * xfrac * (1 / zoom - 1);
}
if(ns || isSubplotConstrained) {
if(!ns) yfrac = 0.5;
for(i = 0; i < ya.length; i++) zoomWheelOneAxis(ya[i], yfrac, zoom);
scrollViewBox[3] *= zoom;
scrollViewBox[1] += scrollViewBox[3] * (1 - yfrac) * (1 / zoom - 1);
}
// viewbox redraw at first
updateSubplots(scrollViewBox);
ticksAndAnnotations(ns, ew);
// then replot after a delay to make sure
// no more scrolling is coming
redrawTimer = setTimeout(function() {
scrollViewBox = [0, 0, pw, ph];
var zoomMode;
if(isSubplotConstrained) zoomMode = 'xy';
else zoomMode = (ew ? 'x' : '') + (ns ? 'y' : '');
dragTail(zoomMode);
}, REDRAWDELAY);
return Lib.pauseEvent(e);
}
// everything but the corners gets wheel zoom
if(ns.length * ew.length !== 1) {
// still seems to be some confusion about onwheel vs onmousewheel...
if(dragger.onwheel !== undefined) dragger.onwheel = zoomWheel;
else if(dragger.onmousewheel !== undefined) dragger.onmousewheel = zoomWheel;
}
// plotDrag: move the plot in response to a drag
function plotDrag(dx, dy) {
// If a transition is in progress, then disable any behavior:
if(gd._transitioningWithDuration) {
return;
}
recomputeAxisLists();
if(xActive === 'ew' || yActive === 'ns') {
if(xActive) dragAxList(xa, dx);
if(yActive) dragAxList(ya, dy);
updateSubplots([xActive ? -dx : 0, yActive ? -dy : 0, pw, ph]);
ticksAndAnnotations(yActive, xActive);
return;
}
// dz: set a new value for one end (0 or 1) of an axis array axArray,
// and return a pixel shift for that end for the viewbox
// based on pixel drag distance d
// TODO: this makes (generally non-fatal) errors when you get
// near floating point limits
function dz(axArray, end, d) {
var otherEnd = 1 - end,
movedAx,
newLinearizedEnd;
for(var i = 0; i < axArray.length; i++) {
var axi = axArray[i];
if(axi.fixedrange) continue;
movedAx = axi;
newLinearizedEnd = axi._rl[otherEnd] +
(axi._rl[end] - axi._rl[otherEnd]) / dZoom(d / axi._length);
var newEnd = axi.l2r(newLinearizedEnd);
// if l2r comes back false or undefined, it means we've dragged off
// the end of valid ranges - so stop.
if(newEnd !== false && newEnd !== undefined) axi.range[end] = newEnd;
}
return movedAx._length * (movedAx._rl[end] - newLinearizedEnd) /
(movedAx._rl[end] - movedAx._rl[otherEnd]);
}
if(isSubplotConstrained && xActive && yActive) {
// dragging a corner of a constrained subplot:
// respect the fixed corner, but harmonize dx and dy
var dxySign = ((xActive === 'w') === (yActive === 'n')) ? 1 : -1;
var dxyFraction = (dx / pw + dxySign * dy / ph) / 2;
dx = dxyFraction * pw;
dy = dxySign * dxyFraction * ph;
}
if(xActive === 'w') dx = dz(xa, 0, dx);
else if(xActive === 'e') dx = dz(xa, 1, -dx);
else if(!xActive) dx = 0;
if(yActive === 'n') dy = dz(ya, 1, dy);
else if(yActive === 's') dy = dz(ya, 0, -dy);
else if(!yActive) dy = 0;
var x0 = (xActive === 'w') ? dx : 0;
var y0 = (yActive === 'n') ? dy : 0;
if(isSubplotConstrained) {
var i;
if(!xActive && yActive.length === 1) {
// dragging one end of the y axis of a constrained subplot
// scale the other axis the same about its middle
for(i = 0; i < xa.length; i++) {
xa[i].range = xa[i]._r.slice();
scaleZoom(xa[i], 1 - dy / ph);
}
dx = dy * pw / ph;
x0 = dx / 2;
}
if(!yActive && xActive.length === 1) {
for(i = 0; i < ya.length; i++) {
ya[i].range = ya[i]._r.slice();
scaleZoom(ya[i], 1 - dx / pw);
}
dy = dx * ph / pw;
y0 = dy / 2;
}
}
updateSubplots([x0, y0, pw - dx, ph - dy]);
ticksAndAnnotations(yActive, xActive);
}
function ticksAndAnnotations(ns, ew) {
var activeAxIds = [],
i;
function pushActiveAxIds(axList) {
for(i = 0; i < axList.length; i++) {
if(!axList[i].fixedrange) activeAxIds.push(axList[i]._id);
}
}
if(ew || isSubplotConstrained) {
pushActiveAxIds(xa);
pushActiveAxIds(xaLinked);
}
if(ns || isSubplotConstrained) {
pushActiveAxIds(ya);
pushActiveAxIds(yaLinked);
}
for(i = 0; i < activeAxIds.length; i++) {
doTicks(gd, activeAxIds[i], true);
}
function redrawObjs(objArray, method, shortCircuit) {
for(i = 0; i < objArray.length; i++) {
var obji = objArray[i];
if((ew && activeAxIds.indexOf(obji.xref) !== -1) ||
(ns && activeAxIds.indexOf(obji.yref) !== -1)) {
method(gd, i);
// once is enough for images (which doesn't use the `i` arg anyway)
if(shortCircuit) return;
}
}
}
// annotations and shapes 'draw' method is slow,
// use the finer-grained 'drawOne' method instead
redrawObjs(fullLayout.annotations || [], Registry.getComponentMethod('annotations', 'drawOne'));
redrawObjs(fullLayout.shapes || [], Registry.getComponentMethod('shapes', 'drawOne'));
redrawObjs(fullLayout.images || [], Registry.getComponentMethod('images', 'draw'), true);
}
function doubleClick() {
if(gd._transitioningWithDuration) return;
var doubleClickConfig = gd._context.doubleClick,
axList = (xActive ? xa : []).concat(yActive ? ya : []),
attrs = {};
var ax, i, rangeInitial;
// For reset+autosize mode:
// If *any* of the main axes is not at its initial range
// (or autoranged, if we have no initial range, to match the logic in
// doubleClickConfig === 'reset' below), we reset.
// If they are *all* at their initial ranges, then we autosize.
if(doubleClickConfig === 'reset+autosize') {
doubleClickConfig = 'autosize';
for(i = 0; i < axList.length; i++) {
ax = axList[i];
if((ax._rangeInitial && (
ax.range[0] !== ax._rangeInitial[0] ||
ax.range[1] !== ax._rangeInitial[1]
)) ||
(!ax._rangeInitial && !ax.autorange)
) {
doubleClickConfig = 'reset';
break;
}
}
}
if(doubleClickConfig === 'autosize') {
// don't set the linked axes here, so relayout marks them as shrinkable
// and we autosize just to the requested axis/axes
for(i = 0; i < axList.length; i++) {
ax = axList[i];
if(!ax.fixedrange) attrs[ax._name + '.autorange'] = true;
}
}
else if(doubleClickConfig === 'reset') {
// when we're resetting, reset all linked axes too, so we get back
// to the fully-auto-with-constraints situation
if(xActive || isSubplotConstrained) axList = axList.concat(xaLinked);
if(yActive && !isSubplotConstrained) axList = axList.concat(yaLinked);
if(isSubplotConstrained) {
if(!xActive) axList = axList.concat(xa);
else if(!yActive) axList = axList.concat(ya);
}
for(i = 0; i < axList.length; i++) {
ax = axList[i];
if(!ax._rangeInitial) {
attrs[ax._name + '.autorange'] = true;
}
else {
rangeInitial = ax._rangeInitial;
attrs[ax._name + '.range[0]'] = rangeInitial[0];
attrs[ax._name + '.range[1]'] = rangeInitial[1];
}
}
}
gd.emit('plotly_doubleclick', null);
Plotly.relayout(gd, attrs);
}
// dragTail - finish a drag event with a redraw
function dragTail(zoommode) {
if(zoommode === undefined) zoommode = (ew ? 'x' : '') + (ns ? 'y' : '');
var attrs = {};
// revert to the previous axis settings, then apply the new ones
// through relayout - this lets relayout manage undo/redo
var axesToModify;
if(zoommode === 'xy') axesToModify = xa.concat(ya);
else if(zoommode === 'x') axesToModify = xa;
else if(zoommode === 'y') axesToModify = ya;
for(var i = 0; i < axesToModify.length; i++) {
var axi = axesToModify[i];
if(axi._r[0] !== axi.range[0]) attrs[axi._name + '.range[0]'] = axi.range[0];
if(axi._r[1] !== axi.range[1]) attrs[axi._name + '.range[1]'] = axi.range[1];
axi.range = axi._input.range = axi._r.slice();
}
updateSubplots([0, 0, pw, ph]);
Plotly.relayout(gd, attrs);
}
// updateSubplots - find all plot viewboxes that should be
// affected by this drag, and update them. look for all plots
// sharing an affected axis (including the one being dragged)
function updateSubplots(viewBox) {
var plotinfos = fullLayout._plots;
var subplots = Object.keys(plotinfos);
var xScaleFactor = viewBox[2] / xa[0]._length;
var yScaleFactor = viewBox[3] / ya[0]._length;
var editX = ew || isSubplotConstrained;
var editY = ns || isSubplotConstrained;
var i, xScaleFactor2, yScaleFactor2, clipDx, clipDy;
// Find the appropriate scaling for this axis, if it's linked to the
// dragged axes by constraints. 0 is special, it means this axis shouldn't
// ever be scaled (will be converted to 1 if the other axis is scaled)
function getLinkedScaleFactor(ax) {
if(ax.fixedrange) return 0;
if(editX && xaLinked.indexOf(ax) !== -1) {
return xScaleFactor;
}
if(editY && (isSubplotConstrained ? xaLinked : yaLinked).indexOf(ax) !== -1) {
return yScaleFactor;
}
return 0;
}
function scaleAndGetShift(ax, scaleFactor) {
if(scaleFactor) {
ax.range = ax._r.slice();
scaleZoom(ax, scaleFactor);
return ax._length * (1 - scaleFactor) / 2;
}
return 0;
}
for(i = 0; i < subplots.length; i++) {
var subplot = plotinfos[subplots[i]],
xa2 = subplot.xaxis,
ya2 = subplot.yaxis,
editX2 = editX && !xa2.fixedrange && (xa.indexOf(xa2) !== -1),
editY2 = editY && !ya2.fixedrange && (ya.indexOf(ya2) !== -1);
if(editX2) {
xScaleFactor2 = xScaleFactor;
clipDx = viewBox[0];
}
else {
xScaleFactor2 = getLinkedScaleFactor(xa2);
clipDx = scaleAndGetShift(xa2, xScaleFactor2);
}
if(editY2) {
yScaleFactor2 = yScaleFactor;
clipDy = viewBox[1];
}
else {
yScaleFactor2 = getLinkedScaleFactor(ya2);
clipDy = scaleAndGetShift(ya2, yScaleFactor2);
}
// don't scale at all if neither axis is scalable here
if(!xScaleFactor2 && !yScaleFactor2) continue;
// but if only one is, reset the other axis scaling
if(!xScaleFactor2) xScaleFactor2 = 1;
if(!yScaleFactor2) yScaleFactor2 = 1;
var plotDx = xa2._offset - clipDx / xScaleFactor2,
plotDy = ya2._offset - clipDy / yScaleFactor2;
fullLayout._defs.selectAll('#' + subplot.clipId)
.call(Drawing.setTranslate, clipDx, clipDy)
.call(Drawing.setScale, xScaleFactor2, yScaleFactor2);
subplot.plot
.call(Drawing.setTranslate, plotDx, plotDy)
.call(Drawing.setScale, 1 / xScaleFactor2, 1 / yScaleFactor2)
// This is specifically directed at scatter traces, applying an inverse
// scale to individual points to counteract the scale of the trace
// as a whole:
.select('.scatterlayer').selectAll('.points').selectAll('.point')
.call(Drawing.setPointGroupScale, xScaleFactor2, yScaleFactor2);
}
}
return dragger;
};
function makeDragger(plotinfo, dragClass, cursor, x, y, w, h) {
var dragger3 = plotinfo.draglayer.selectAll('.' + dragClass).data([0]);
dragger3.enter().append('rect')
.classed('drag', true)
.classed(dragClass, true)
.style({fill: 'transparent', 'stroke-width': 0})
.attr('data-subplot', plotinfo.id);
dragger3.call(Drawing.setRect, x, y, w, h)
.call(setCursor, cursor);
return dragger3.node();
}
function isDirectionActive(axList, activeVal) {
for(var i = 0; i < axList.length; i++) {
if(!axList[i].fixedrange) return activeVal;
}
return '';
}
function getEndText(ax, end) {
var initialVal = ax.range[end],
diff = Math.abs(initialVal - ax.range[1 - end]),
dig;
// TODO: this should basically be ax.r2d but we're doing extra
// rounding here... can we clean up at all?
if(ax.type === 'date') {
return initialVal;
}
else if(ax.type === 'log') {
dig = Math.ceil(Math.max(0, -Math.log(diff) / Math.LN10)) + 3;
return d3.format('.' + dig + 'g')(Math.pow(10, initialVal));
}
else { // linear numeric (or category... but just show numbers here)
dig = Math.floor(Math.log(Math.abs(initialVal)) / Math.LN10) -
Math.floor(Math.log(diff) / Math.LN10) + 4;
return d3.format('.' + String(dig) + 'g')(initialVal);
}
}
function zoomAxRanges(axList, r0Fraction, r1Fraction, linkedAxes) {
var i,
axi,
axRangeLinear0,
axRangeLinearSpan;
for(i = 0; i < axList.length; i++) {
axi = axList[i];
if(axi.fixedrange) continue;
axRangeLinear0 = axi._rl[0];
axRangeLinearSpan = axi._rl[1] - axRangeLinear0;
axi.range = [
axi.l2r(axRangeLinear0 + axRangeLinearSpan * r0Fraction),
axi.l2r(axRangeLinear0 + axRangeLinearSpan * r1Fraction)
];
}
// zoom linked axes about their centers
if(linkedAxes && linkedAxes.length) {
var linkedR0Fraction = (r0Fraction + (1 - r1Fraction)) / 2;
zoomAxRanges(linkedAxes, linkedR0Fraction, 1 - linkedR0Fraction);
}
}
function dragAxList(axList, pix) {
for(var i = 0; i < axList.length; i++) {
var axi = axList[i];
if(!axi.fixedrange) {
axi.range = [
axi.l2r(axi._rl[0] - pix / axi._m),
axi.l2r(axi._rl[1] - pix / axi._m)
];
}
}
}
// common transform for dragging one end of an axis
// d>0 is compressing scale (cursor is over the plot,
// the axis end should move with the cursor)
// d<0 is expanding (cursor is off the plot, axis end moves
// nonlinearly so you can expand far)
function dZoom(d) {
return 1 - ((d >= 0) ? Math.min(d, 0.9) :
1 / (1 / Math.max(d, -0.3) + 3.222));
}
function getDragCursor(nsew, dragmode) {
if(!nsew) return 'pointer';
if(nsew === 'nsew') {
if(dragmode === 'pan') return 'move';
return 'crosshair';
}
return nsew.toLowerCase() + '-resize';
}
function makeZoombox(zoomlayer, lum, xs, ys, path0) {
return zoomlayer.append('path')
.attr('class', 'zoombox')
.style({
'fill': lum > 0.2 ? 'rgba(0,0,0,0)' : 'rgba(255,255,255,0)',
'stroke-width': 0
})
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', path0 + 'Z');
}
function makeCorners(zoomlayer, xs, ys) {
return zoomlayer.append('path')
.attr('class', 'zoombox-corners')
.style({
fill: Color.background,
stroke: Color.defaultLine,
'stroke-width': 1,
opacity: 0
})
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', 'M0,0Z');
}
function clearSelect(zoomlayer) {
// until we get around to persistent selections, remove the outline
// here. The selection itself will be removed when the plot redraws
// at the end.
zoomlayer.selectAll('.select-outline').remove();
}
function updateZoombox(zb, corners, box, path0, dimmed, lum) {
zb.attr('d',
path0 + 'M' + (box.l) + ',' + (box.t) + 'v' + (box.h) +
'h' + (box.w) + 'v-' + (box.h) + 'h-' + (box.w) + 'Z');
if(!dimmed) {
zb.transition()
.style('fill', lum > 0.2 ? 'rgba(0,0,0,0.4)' :
'rgba(255,255,255,0.3)')
.duration(200);
corners.transition()
.style('opacity', 1)
.duration(200);
}
}
function removeZoombox(gd) {
d3.select(gd)
.selectAll('.zoombox,.js-zoombox-backdrop,.js-zoombox-menu,.zoombox-corners')
.remove();
}
function isSelectOrLasso(dragmode) {
var modes = ['lasso', 'select'];
return modes.indexOf(dragmode) !== -1;
}
function xCorners(box, y0) {
return 'M' +
(box.l - 0.5) + ',' + (y0 - MINZOOM - 0.5) +
'h-3v' + (2 * MINZOOM + 1) + 'h3ZM' +
(box.r + 0.5) + ',' + (y0 - MINZOOM - 0.5) +
'h3v' + (2 * MINZOOM + 1) + 'h-3Z';
}
function yCorners(box, x0) {
return 'M' +
(x0 - MINZOOM - 0.5) + ',' + (box.t - 0.5) +
'v-3h' + (2 * MINZOOM + 1) + 'v3ZM' +
(x0 - MINZOOM - 0.5) + ',' + (box.b + 0.5) +
'v3h' + (2 * MINZOOM + 1) + 'v-3Z';
}
function xyCorners(box) {
var clen = Math.floor(Math.min(box.b - box.t, box.r - box.l, MINZOOM) / 2);
return 'M' +
(box.l - 3.5) + ',' + (box.t - 0.5 + clen) + 'h3v' + (-clen) +
'h' + clen + 'v-3h-' + (clen + 3) + 'ZM' +
(box.r + 3.5) + ',' + (box.t - 0.5 + clen) + 'h-3v' + (-clen) +
'h' + (-clen) + 'v-3h' + (clen + 3) + 'ZM' +
(box.r + 3.5) + ',' + (box.b + 0.5 - clen) + 'h-3v' + clen +
'h' + (-clen) + 'v3h' + (clen + 3) + 'ZM' +
(box.l - 3.5) + ',' + (box.b + 0.5 - clen) + 'h3v' + clen +
'h' + clen + 'v3h-' + (clen + 3) + 'Z';
}
function calcLinks(constraintGroups, xIDs, yIDs) {
var isSubplotConstrained = false;
var xLinks = {};
var yLinks = {};
var i, j, k;
var group, xLinkID, yLinkID;
for(i = 0; i < constraintGroups.length; i++) {
group = constraintGroups[i];
// check if any of the x axes we're dragging is in this constraint group
for(j = 0; j < xIDs.length; j++) {
if(group[xIDs[j]]) {
// put the rest of these axes into xLinks, if we're not already
// dragging them, so we know to scale these axes automatically too
// to match the changes in the dragged x axes
for(xLinkID in group) {
if((xLinkID.charAt(0) === 'x' ? xIDs : yIDs).indexOf(xLinkID) === -1) {
xLinks[xLinkID] = 1;
}
}
// check if the x and y axes of THIS drag are linked
for(k = 0; k < yIDs.length; k++) {
if(group[yIDs[k]]) isSubplotConstrained = true;
}
}
}
// now check if any of the y axes we're dragging is in this constraint group
// only look for outside links, as we've already checked for links within the dragger
for(j = 0; j < yIDs.length; j++) {
if(group[yIDs[j]]) {
for(yLinkID in group) {
if((yLinkID.charAt(0) === 'x' ? xIDs : yIDs).indexOf(yLinkID) === -1) {
yLinks[yLinkID] = 1;
}
}
}
}
}
if(isSubplotConstrained) {
// merge xLinks and yLinks if the subplot is constrained,
// since we'll always apply both anyway and the two will contain
// duplicates
Lib.extendFlat(xLinks, yLinks);
yLinks = {};
}
return {
x: xLinks,
y: yLinks,
xy: isSubplotConstrained
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var tinycolor = require('tinycolor2');
var Lib = require('../../lib');
var Events = require('../../lib/events');
var svgTextUtils = require('../../lib/svg_text_utils');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var dragElement = require('../../components/dragelement');
var overrideCursor = require('../../lib/override_cursor');
var Registry = require('../../registry');
var Axes = require('./axes');
var constants = require('./constants');
var dragBox = require('./dragbox');
var layoutAttributes = require('../layout_attributes');
var fx = module.exports = {};
// TODO remove this in version 2.0
// copy on Fx for backward compatible
fx.unhover = dragElement.unhover;
fx.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
function coerce(attr, dflt) {
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
}
coerce('dragmode');
var hovermodeDflt;
if(layoutOut._has('cartesian')) {
// flag for 'horizontal' plots:
// determines the state of the mode bar 'compare' hovermode button
var isHoriz = layoutOut._isHoriz = fx.isHoriz(fullData);
hovermodeDflt = isHoriz ? 'y' : 'x';
}
else hovermodeDflt = 'closest';
coerce('hovermode', hovermodeDflt);
};
fx.isHoriz = function(fullData) {
var isHoriz = true;
for(var i = 0; i < fullData.length; i++) {
var trace = fullData[i];
if(trace.orientation !== 'h') {
isHoriz = false;
break;
}
}
return isHoriz;
};
fx.init = function(gd) {
var fullLayout = gd._fullLayout;
if(!fullLayout._has('cartesian') || gd._context.staticPlot) return;
var subplots = Object.keys(fullLayout._plots || {}).sort(function(a, b) {
// sort overlays last, then by x axis number, then y axis number
if((fullLayout._plots[a].mainplot && true) ===
(fullLayout._plots[b].mainplot && true)) {
var aParts = a.split('y'),
bParts = b.split('y');
return (aParts[0] === bParts[0]) ?
(Number(aParts[1] || 1) - Number(bParts[1] || 1)) :
(Number(aParts[0] || 1) - Number(bParts[0] || 1));
}
return fullLayout._plots[a].mainplot ? 1 : -1;
});
subplots.forEach(function(subplot) {
var plotinfo = fullLayout._plots[subplot];
if(!fullLayout._has('cartesian')) return;
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis,
// the y position of the main x axis line
y0 = (xa._linepositions[subplot] || [])[3],
// the x position of the main y axis line
x0 = (ya._linepositions[subplot] || [])[3];
var DRAGGERSIZE = constants.DRAGGERSIZE;
if(isNumeric(y0) && xa.side === 'top') y0 -= DRAGGERSIZE;
if(isNumeric(x0) && ya.side !== 'right') x0 -= DRAGGERSIZE;
// main and corner draggers need not be repeated for
// overlaid subplots - these draggers drag them all
if(!plotinfo.mainplot) {
// main dragger goes over the grids and data, so we use its
// mousemove events for all data hover effects
var maindrag = dragBox(gd, plotinfo, 0, 0,
xa._length, ya._length, 'ns', 'ew');
maindrag.onmousemove = function(evt) {
// This is on `gd._fullLayout`, *not* fullLayout because the reference
// changes by the time this is called again.
gd._fullLayout._rehover = function() {
if(gd._fullLayout._hoversubplot === subplot) {
fx.hover(gd, evt, subplot);
}
};
fx.hover(gd, evt, subplot);
// Note that we have *not* used the cached fullLayout variable here
// since that may be outdated when this is called as a callback later on
gd._fullLayout._lasthover = maindrag;
gd._fullLayout._hoversubplot = subplot;
};
/*
* IMPORTANT:
* We must check for the presence of the drag cover here.
* If we don't, a 'mouseout' event is triggered on the
* maindrag before each 'click' event, which has the effect
* of clearing the hoverdata; thus, cancelling the click event.
*/
maindrag.onmouseout = function(evt) {
if(gd._dragging) return;
// When the mouse leaves this maindrag, unset the hovered subplot.
// This may cause problems if it leaves the subplot directly *onto*
// another subplot, but that's a tiny corner case at the moment.
gd._fullLayout._hoversubplot = null;
dragElement.unhover(gd, evt);
};
maindrag.onclick = function(evt) {
fx.click(gd, evt);
};
// corner draggers
if(gd._context.showAxisDragHandles) {
dragBox(gd, plotinfo, -DRAGGERSIZE, -DRAGGERSIZE,
DRAGGERSIZE, DRAGGERSIZE, 'n', 'w');
dragBox(gd, plotinfo, xa._length, -DRAGGERSIZE,
DRAGGERSIZE, DRAGGERSIZE, 'n', 'e');
dragBox(gd, plotinfo, -DRAGGERSIZE, ya._length,
DRAGGERSIZE, DRAGGERSIZE, 's', 'w');
dragBox(gd, plotinfo, xa._length, ya._length,
DRAGGERSIZE, DRAGGERSIZE, 's', 'e');
}
}
if(gd._context.showAxisDragHandles) {
// x axis draggers - if you have overlaid plots,
// these drag each axis separately
if(isNumeric(y0)) {
if(xa.anchor === 'free') y0 -= fullLayout._size.h * (1 - ya.domain[1]);
dragBox(gd, plotinfo, xa._length * 0.1, y0,
xa._length * 0.8, DRAGGERSIZE, '', 'ew');
dragBox(gd, plotinfo, 0, y0,
xa._length * 0.1, DRAGGERSIZE, '', 'w');
dragBox(gd, plotinfo, xa._length * 0.9, y0,
xa._length * 0.1, DRAGGERSIZE, '', 'e');
}
// y axis draggers
if(isNumeric(x0)) {
if(ya.anchor === 'free') x0 -= fullLayout._size.w * xa.domain[0];
dragBox(gd, plotinfo, x0, ya._length * 0.1,
DRAGGERSIZE, ya._length * 0.8, 'ns', '');
dragBox(gd, plotinfo, x0, ya._length * 0.9,
DRAGGERSIZE, ya._length * 0.1, 's', '');
dragBox(gd, plotinfo, x0, 0,
DRAGGERSIZE, ya._length * 0.1, 'n', '');
}
}
});
// In case you mousemove over some hovertext, send it to fx.hover too
// we do this so that we can put the hover text in front of everything,
// but still be able to interact with everything as if it isn't there
var hoverLayer = fullLayout._hoverlayer.node();
hoverLayer.onmousemove = function(evt) {
evt.target = fullLayout._lasthover;
fx.hover(gd, evt, fullLayout._hoversubplot);
};
hoverLayer.onclick = function(evt) {
evt.target = fullLayout._lasthover;
fx.click(gd, evt);
};
// also delegate mousedowns... TODO: does this actually work?
hoverLayer.onmousedown = function(evt) {
fullLayout._lasthover.onmousedown(evt);
};
};
// hover labels for multiple horizontal bars get tilted by some angle,
// then need to be offset differently if they overlap
var YANGLE = constants.YANGLE,
YA_RADIANS = Math.PI * YANGLE / 180,
// expansion of projected height
YFACTOR = 1 / Math.sin(YA_RADIANS),
// to make the appropriate post-rotation x offset,
// you need both x and y offsets
YSHIFTX = Math.cos(YA_RADIANS),
YSHIFTY = Math.sin(YA_RADIANS);
// convenience functions for mapping all relevant axes
function flat(subplots, v) {
var out = [];
for(var i = subplots.length; i > 0; i--) out.push(v);
return out;
}
function p2c(axArray, v) {
var out = [];
for(var i = 0; i < axArray.length; i++) out.push(axArray[i].p2c(v));
return out;
}
function quadrature(dx, dy) {
return function(di) {
var x = dx(di),
y = dy(di);
return Math.sqrt(x * x + y * y);
};
}
// size and display constants for hover text
var HOVERARROWSIZE = constants.HOVERARROWSIZE,
HOVERTEXTPAD = constants.HOVERTEXTPAD;
// fx.hover: highlight data on hover
// evt can be a mousemove event, or an object with data about what points
// to hover on
// {xpx,ypx[,hovermode]} - pixel locations from top left
// (with optional overriding hovermode)
// {xval,yval[,hovermode]} - data values
// [{curveNumber,(pointNumber|xval and/or yval)}] -
// array of specific points to highlight
// pointNumber is a single integer if gd.data[curveNumber] is 1D,
// or a two-element array if it's 2D
// xval and yval are data values,
// 1D data may specify either or both,
// 2D data must specify both
// subplot is an id string (default "xy")
// makes use of gl.hovermode, which can be:
// x (find the points with the closest x values, ie a column),
// closest (find the single closest point)
// internally there are two more that occasionally get used:
// y (pick out a row - only used for multiple horizontal bar charts)
// array (used when the user specifies an explicit
// array of points to hover on)
//
// We wrap the hovers in a timer, to limit their frequency.
// The actual rendering is done by private functions
// hover() and unhover().
fx.hover = function(gd, evt, subplot) {
if(typeof gd === 'string') gd = document.getElementById(gd);
if(gd._lastHoverTime === undefined) gd._lastHoverTime = 0;
// If we have an update queued, discard it now
if(gd._hoverTimer !== undefined) {
clearTimeout(gd._hoverTimer);
gd._hoverTimer = undefined;
}
// Is it more than 100ms since the last update? If so, force
// an update now (synchronously) and exit
if(Date.now() > gd._lastHoverTime + constants.HOVERMINTIME) {
hover(gd, evt, subplot);
gd._lastHoverTime = Date.now();
return;
}
// Queue up the next hover for 100ms from now (if no further events)
gd._hoverTimer = setTimeout(function() {
hover(gd, evt, subplot);
gd._lastHoverTime = Date.now();
gd._hoverTimer = undefined;
}, constants.HOVERMINTIME);
};
// The actual implementation is here:
function hover(gd, evt, subplot) {
if(subplot === 'pie') {
gd.emit('plotly_hover', {
event: evt.originalEvent,
points: [evt]
});
return;
}
if(!subplot) subplot = 'xy';
// if the user passed in an array of subplots,
// use those instead of finding overlayed plots
var subplots = Array.isArray(subplot) ? subplot : [subplot];
var fullLayout = gd._fullLayout,
plots = fullLayout._plots || [],
plotinfo = plots[subplot];
// list of all overlaid subplots to look at
if(plotinfo) {
var overlayedSubplots = plotinfo.overlays.map(function(pi) {
return pi.id;
});
subplots = subplots.concat(overlayedSubplots);
}
var len = subplots.length,
xaArray = new Array(len),
yaArray = new Array(len);
for(var i = 0; i < len; i++) {
var spId = subplots[i];
// 'cartesian' case
var plotObj = plots[spId];
if(plotObj) {
// TODO make sure that fullLayout_plots axis refs
// get updated properly so that we don't have
// to use Axes.getFromId in general.
xaArray[i] = Axes.getFromId(gd, plotObj.xaxis._id);
yaArray[i] = Axes.getFromId(gd, plotObj.yaxis._id);
continue;
}
// other subplot types
var _subplot = fullLayout[spId]._subplot;
xaArray[i] = _subplot.xaxis;
yaArray[i] = _subplot.yaxis;
}
var hovermode = evt.hovermode || fullLayout.hovermode;
if(['x', 'y', 'closest'].indexOf(hovermode) === -1 || !gd.calcdata ||
gd.querySelector('.zoombox') || gd._dragging) {
return dragElement.unhoverRaw(gd, evt);
}
// hoverData: the set of candidate points we've found to highlight
var hoverData = [],
// searchData: the data to search in. Mostly this is just a copy of
// gd.calcdata, filtered to the subplot and overlays we're on
// but if a point array is supplied it will be a mapping
// of indicated curves
searchData = [],
// [x|y]valArray: the axis values of the hover event
// mapped onto each of the currently selected overlaid subplots
xvalArray,
yvalArray,
// used in loops
itemnum,
curvenum,
cd,
trace,
subplotId,
subploti,
mode,
xval,
yval,
pointData,
closedataPreviousLength;
// Figure out what we're hovering on:
// mouse location or user-supplied data
if(Array.isArray(evt)) {
// user specified an array of points to highlight
hovermode = 'array';
for(itemnum = 0; itemnum < evt.length; itemnum++) {
cd = gd.calcdata[evt[itemnum].curveNumber||0];
if(cd[0].trace.hoverinfo !== 'skip') {
searchData.push(cd);
}
}
}
else {
for(curvenum = 0; curvenum < gd.calcdata.length; curvenum++) {
cd = gd.calcdata[curvenum];
trace = cd[0].trace;
if(trace.hoverinfo !== 'skip' && subplots.indexOf(getSubplot(trace)) !== -1) {
searchData.push(cd);
}
}
// [x|y]px: the pixels (from top left) of the mouse location
// on the currently selected plot area
var hasUserCalledHover = !evt.target,
xpx, ypx;
if(hasUserCalledHover) {
if('xpx' in evt) xpx = evt.xpx;
else xpx = xaArray[0]._length / 2;
if('ypx' in evt) ypx = evt.ypx;
else ypx = yaArray[0]._length / 2;
}
else {
// fire the beforehover event and quit if it returns false
// note that we're only calling this on real mouse events, so
// manual calls to fx.hover will always run.
if(Events.triggerHandler(gd, 'plotly_beforehover', evt) === false) {
return;
}
var dbb = evt.target.getBoundingClientRect();
xpx = evt.clientX - dbb.left;
ypx = evt.clientY - dbb.top;
// in case hover was called from mouseout into hovertext,
// it's possible you're not actually over the plot anymore
if(xpx < 0 || xpx > dbb.width || ypx < 0 || ypx > dbb.height) {
return dragElement.unhoverRaw(gd, evt);
}
}
if('xval' in evt) xvalArray = flat(subplots, evt.xval);
else xvalArray = p2c(xaArray, xpx);
if('yval' in evt) yvalArray = flat(subplots, evt.yval);
else yvalArray = p2c(yaArray, ypx);
if(!isNumeric(xvalArray[0]) || !isNumeric(yvalArray[0])) {
Lib.warn('Fx.hover failed', evt, gd);
return dragElement.unhoverRaw(gd, evt);
}
}
// the pixel distance to beat as a matching point
// in 'x' or 'y' mode this resets for each trace
var distance = Infinity;
// find the closest point in each trace
// this is minimum dx and/or dy, depending on mode
// and the pixel position for the label (labelXpx, labelYpx)
for(curvenum = 0; curvenum < searchData.length; curvenum++) {
cd = searchData[curvenum];
// filter out invisible or broken data
if(!cd || !cd[0] || !cd[0].trace || cd[0].trace.visible !== true) continue;
trace = cd[0].trace;
// Explicitly bail out for these two. I don't know how to otherwise prevent
// the rest of this function from running and failing
if(['carpet', 'contourcarpet'].indexOf(trace._module.name) !== -1) continue;
subplotId = getSubplot(trace);
subploti = subplots.indexOf(subplotId);
// within one trace mode can sometimes be overridden
mode = hovermode;
// container for new point, also used to pass info into module.hoverPoints
pointData = {
// trace properties
cd: cd,
trace: trace,
xa: xaArray[subploti],
ya: yaArray[subploti],
name: (gd.data.length > 1 || trace.hoverinfo.indexOf('name') !== -1) ? trace.name : undefined,
// point properties - override all of these
index: false, // point index in trace - only used by plotly.js hoverdata consumers
distance: Math.min(distance, constants.MAXDIST), // pixel distance or pseudo-distance
color: Color.defaultLine, // trace color
x0: undefined,
x1: undefined,
y0: undefined,
y1: undefined,
xLabelVal: undefined,
yLabelVal: undefined,
zLabelVal: undefined,
text: undefined
};
// add ref to subplot object (non-cartesian case)
if(fullLayout[subplotId]) {
pointData.subplot = fullLayout[subplotId]._subplot;
}
closedataPreviousLength = hoverData.length;
// for a highlighting array, figure out what
// we're searching for with this element
if(mode === 'array') {
var selection = evt[curvenum];
if('pointNumber' in selection) {
pointData.index = selection.pointNumber;
mode = 'closest';
}
else {
mode = '';
if('xval' in selection) {
xval = selection.xval;
mode = 'x';
}
if('yval' in selection) {
yval = selection.yval;
mode = mode ? 'closest' : 'y';
}
}
}
else {
xval = xvalArray[subploti];
yval = yvalArray[subploti];
}
// Now find the points.
if(trace._module && trace._module.hoverPoints) {
var newPoints = trace._module.hoverPoints(pointData, xval, yval, mode);
if(newPoints) {
var newPoint;
for(var newPointNum = 0; newPointNum < newPoints.length; newPointNum++) {
newPoint = newPoints[newPointNum];
if(isNumeric(newPoint.x0) && isNumeric(newPoint.y0)) {
hoverData.push(cleanPoint(newPoint, hovermode));
}
}
}
}
else {
Lib.log('Unrecognized trace type in hover:', trace);
}
// in closest mode, remove any existing (farther) points
// and don't look any farther than this latest point (or points, if boxes)
if(hovermode === 'closest' && hoverData.length > closedataPreviousLength) {
hoverData.splice(0, closedataPreviousLength);
distance = hoverData[0].distance;
}
}
// nothing left: remove all labels and quit
if(hoverData.length === 0) return dragElement.unhoverRaw(gd, evt);
hoverData.sort(function(d1, d2) { return d1.distance - d2.distance; });
// lastly, emit custom hover/unhover events
var oldhoverdata = gd._hoverdata,
newhoverdata = [];
// pull out just the data that's useful to
// other people and send it to the event
for(itemnum = 0; itemnum < hoverData.length; itemnum++) {
var pt = hoverData[itemnum];
var out = {
data: pt.trace._input,
fullData: pt.trace,
curveNumber: pt.trace.index,
pointNumber: pt.index
};
if(pt.trace._module.eventData) out = pt.trace._module.eventData(out, pt);
else {
out.x = pt.xVal;
out.y = pt.yVal;
out.xaxis = pt.xa;
out.yaxis = pt.ya;
if(pt.zLabelVal !== undefined) out.z = pt.zLabelVal;
}
newhoverdata.push(out);
}
gd._hoverdata = newhoverdata;
if(hoverChanged(gd, evt, oldhoverdata) && fullLayout._hasCartesian) {
var spikelineOpts = {
hovermode: hovermode,
fullLayout: fullLayout,
container: fullLayout._hoverlayer,
outerContainer: fullLayout._paperdiv
};
createSpikelines(hoverData, spikelineOpts);
}
// if there's more than one horz bar trace,
// rotate the labels so they don't overlap
var rotateLabels = hovermode === 'y' && searchData.length > 1;
var bgColor = Color.combine(
fullLayout.plot_bgcolor || Color.background,
fullLayout.paper_bgcolor
);
var labelOpts = {
hovermode: hovermode,
rotateLabels: rotateLabels,
bgColor: bgColor,
container: fullLayout._hoverlayer,
outerContainer: fullLayout._paperdiv
};
var hoverLabels = createHoverText(hoverData, labelOpts);
hoverAvoidOverlaps(hoverData, rotateLabels ? 'xa' : 'ya');
alignHoverText(hoverLabels, rotateLabels);
// TODO: tagName hack is needed to appease geo.js's hack of using evt.target=true
// we should improve the "fx" API so other plots can use it without these hack.
if(evt.target && evt.target.tagName) {
var hasClickToShow = Registry.getComponentMethod('annotations', 'hasClickToShow')(gd, newhoverdata);
overrideCursor(d3.select(evt.target), hasClickToShow ? 'pointer' : '');
}
// don't emit events if called manually
if(!evt.target || !hoverChanged(gd, evt, oldhoverdata)) return;
if(oldhoverdata) {
gd.emit('plotly_unhover', {
event: evt,
points: oldhoverdata
});
}
gd.emit('plotly_hover', {
event: evt,
points: gd._hoverdata,
xaxes: xaArray,
yaxes: yaArray,
xvals: xvalArray,
yvals: yvalArray
});
}
// look for either .subplot (currently just ternary)
// or xaxis and yaxis attributes
function getSubplot(trace) {
return trace.subplot || (trace.xaxis + trace.yaxis) || trace.geo;
}
fx.getDistanceFunction = function(mode, dx, dy, dxy) {
if(mode === 'closest') return dxy || quadrature(dx, dy);
return mode === 'x' ? dx : dy;
};
fx.getClosest = function(cd, distfn, pointData) {
// do we already have a point number? (array mode only)
if(pointData.index !== false) {
if(pointData.index >= 0 && pointData.index < cd.length) {
pointData.distance = 0;
}
else pointData.index = false;
}
else {
// apply the distance function to each data point
// this is the longest loop... if this bogs down, we may need
// to create pre-sorted data (by x or y), not sure how to
// do this for 'closest'
for(var i = 0; i < cd.length; i++) {
var newDistance = distfn(cd[i]);
if(newDistance <= pointData.distance) {
pointData.index = i;
pointData.distance = newDistance;
}
}
}
return pointData;
};
function cleanPoint(d, hovermode) {
d.posref = hovermode === 'y' ? (d.x0 + d.x1) / 2 : (d.y0 + d.y1) / 2;
// then constrain all the positions to be on the plot
d.x0 = Lib.constrain(d.x0, 0, d.xa._length);
d.x1 = Lib.constrain(d.x1, 0, d.xa._length);
d.y0 = Lib.constrain(d.y0, 0, d.ya._length);
d.y1 = Lib.constrain(d.y1, 0, d.ya._length);
// and convert the x and y label values into objects
// formatted as text, with font info
var logOffScale;
if(d.xLabelVal !== undefined) {
logOffScale = (d.xa.type === 'log' && d.xLabelVal <= 0);
var xLabelObj = Axes.tickText(d.xa,
d.xa.c2l(logOffScale ? -d.xLabelVal : d.xLabelVal), 'hover');
if(logOffScale) {
if(d.xLabelVal === 0) d.xLabel = '0';
else d.xLabel = '-' + xLabelObj.text;
}
// TODO: should we do something special if the axis calendar and
// the data calendar are different? Somehow display both dates with
// their system names? Right now it will just display in the axis calendar
// but users could add the other one as text.
else d.xLabel = xLabelObj.text;
d.xVal = d.xa.c2d(d.xLabelVal);
}
if(d.yLabelVal !== undefined) {
logOffScale = (d.ya.type === 'log' && d.yLabelVal <= 0);
var yLabelObj = Axes.tickText(d.ya,
d.ya.c2l(logOffScale ? -d.yLabelVal : d.yLabelVal), 'hover');
if(logOffScale) {
if(d.yLabelVal === 0) d.yLabel = '0';
else d.yLabel = '-' + yLabelObj.text;
}
// TODO: see above TODO
else d.yLabel = yLabelObj.text;
d.yVal = d.ya.c2d(d.yLabelVal);
}
if(d.zLabelVal !== undefined) d.zLabel = String(d.zLabelVal);
// for box means and error bars, add the range to the label
if(!isNaN(d.xerr) && !(d.xa.type === 'log' && d.xerr <= 0)) {
var xeText = Axes.tickText(d.xa, d.xa.c2l(d.xerr), 'hover').text;
if(d.xerrneg !== undefined) {
d.xLabel += ' +' + xeText + ' / -' +
Axes.tickText(d.xa, d.xa.c2l(d.xerrneg), 'hover').text;
}
else d.xLabel += ' ± ' + xeText;
// small distance penalty for error bars, so that if there are
// traces with errors and some without, the error bar label will
// hoist up to the point
if(hovermode === 'x') d.distance += 1;
}
if(!isNaN(d.yerr) && !(d.ya.type === 'log' && d.yerr <= 0)) {
var yeText = Axes.tickText(d.ya, d.ya.c2l(d.yerr), 'hover').text;
if(d.yerrneg !== undefined) {
d.yLabel += ' +' + yeText + ' / -' +
Axes.tickText(d.ya, d.ya.c2l(d.yerrneg), 'hover').text;
}
else d.yLabel += ' ± ' + yeText;
if(hovermode === 'y') d.distance += 1;
}
var infomode = d.trace.hoverinfo;
if(infomode !== 'all') {
infomode = infomode.split('+');
if(infomode.indexOf('x') === -1) d.xLabel = undefined;
if(infomode.indexOf('y') === -1) d.yLabel = undefined;
if(infomode.indexOf('z') === -1) d.zLabel = undefined;
if(infomode.indexOf('text') === -1) d.text = undefined;
if(infomode.indexOf('name') === -1) d.name = undefined;
}
return d;
}
/*
* Draw a single hover item in a pre-existing svg container somewhere
* hoverItem should have keys:
* - x and y (or x0, x1, y0, and y1):
* the pixel position to mark, relative to opts.container
* - xLabel, yLabel, zLabel, text, and name:
* info to go in the label
* - color:
* the background color for the label.
* - idealAlign (optional):
* 'left' or 'right' for which side of the x/y box to try to put this on first
* - borderColor (optional):
* color for the border, defaults to strongest contrast with color
* - fontFamily (optional):
* string, the font for this label, defaults to constants.HOVERFONT
* - fontSize (optional):
* the label font size, defaults to constants.HOVERFONTSIZE
* - fontColor (optional):
* defaults to borderColor
* opts should have keys:
* - bgColor:
* the background color this is against, used if the trace is
* non-opaque, and for the name, which goes outside the box
* - container:
* a <svg> or <g> element to add the hover label to
* - outerContainer:
* normally a parent of `container`, sets the bounding box to use to
* constrain the hover label and determine whether to show it on the left or right
*/
fx.loneHover = function(hoverItem, opts) {
var pointData = {
color: hoverItem.color || Color.defaultLine,
x0: hoverItem.x0 || hoverItem.x || 0,
x1: hoverItem.x1 || hoverItem.x || 0,
y0: hoverItem.y0 || hoverItem.y || 0,
y1: hoverItem.y1 || hoverItem.y || 0,
xLabel: hoverItem.xLabel,
yLabel: hoverItem.yLabel,
zLabel: hoverItem.zLabel,
text: hoverItem.text,
name: hoverItem.name,
idealAlign: hoverItem.idealAlign,
// optional extra bits of styling
borderColor: hoverItem.borderColor,
fontFamily: hoverItem.fontFamily,
fontSize: hoverItem.fontSize,
fontColor: hoverItem.fontColor,
// filler to make createHoverText happy
trace: {
index: 0,
hoverinfo: ''
},
xa: {_offset: 0},
ya: {_offset: 0},
index: 0
};
var container3 = d3.select(opts.container),
outerContainer3 = opts.outerContainer ?
d3.select(opts.outerContainer) : container3;
var fullOpts = {
hovermode: 'closest',
rotateLabels: false,
bgColor: opts.bgColor || Color.background,
container: container3,
outerContainer: outerContainer3
};
var hoverLabel = createHoverText([pointData], fullOpts);
alignHoverText(hoverLabel, fullOpts.rotateLabels);
return hoverLabel.node();
};
fx.loneUnhover = function(containerOrSelection) {
// duck type whether the arg is a d3 selection because ie9 doesn't
// handle instanceof like modern browsers do.
var selection = Lib.isD3Selection(containerOrSelection) ?
containerOrSelection :
d3.select(containerOrSelection);
selection.selectAll('g.hovertext').remove();
selection.selectAll('.spikeline').remove();
};
function createSpikelines(hoverData, opts) {
var hovermode = opts.hovermode;
var container = opts.container;
var c0 = hoverData[0];
var xa = c0.xa;
var ya = c0.ya;
var showX = xa.showspikes;
var showY = ya.showspikes;
// Remove old spikeline items
container.selectAll('.spikeline').remove();
if(hovermode !== 'closest' || !(showX || showY)) return;
var fullLayout = opts.fullLayout;
var xPoint = xa._offset + (c0.x0 + c0.x1) / 2;
var yPoint = ya._offset + (c0.y0 + c0.y1) / 2;
var contrastColor = Color.combine(fullLayout.plot_bgcolor, fullLayout.paper_bgcolor);
var dfltDashColor = tinycolor.readability(c0.color, contrastColor) < 1.5 ?
Color.contrast(contrastColor) : c0.color;
if(showY) {
var yMode = ya.spikemode;
var yThickness = ya.spikethickness;
var yColor = ya.spikecolor || dfltDashColor;
var yBB = ya._boundingBox;
var xEdge = ((yBB.left + yBB.right) / 2) < xPoint ? yBB.right : yBB.left;
if(yMode.indexOf('toaxis') !== -1 || yMode.indexOf('across') !== -1) {
var xBase = xEdge;
var xEndSpike = xPoint;
if(yMode.indexOf('across') !== -1) {
xBase = ya._counterSpan[0];
xEndSpike = ya._counterSpan[1];
}
// Background horizontal Line (to y-axis)
container.append('line')
.attr({
'x1': xBase,
'x2': xEndSpike,
'y1': yPoint,
'y2': yPoint,
'stroke-width': yThickness + 2,
'stroke': contrastColor
})
.classed('spikeline', true)
.classed('crisp', true);
// Foreground horizontal line (to y-axis)
container.append('line')
.attr({
'x1': xBase,
'x2': xEndSpike,
'y1': yPoint,
'y2': yPoint,
'stroke-width': yThickness,
'stroke': yColor,
'stroke-dasharray': Drawing.dashStyle(ya.spikedash, yThickness)
})
.classed('spikeline', true)
.classed('crisp', true);
}
// Y axis marker
if(yMode.indexOf('marker') !== -1) {
container.append('circle')
.attr({
'cx': xEdge + (ya.side !== 'right' ? yThickness : -yThickness),
'cy': yPoint,
'r': yThickness,
'fill': yColor
})
.classed('spikeline', true);
}
}
if(showX) {
var xMode = xa.spikemode;
var xThickness = xa.spikethickness;
var xColor = xa.spikecolor || dfltDashColor;
var xBB = xa._boundingBox;
var yEdge = ((xBB.top + xBB.bottom) / 2) < yPoint ? xBB.bottom : xBB.top;
if(xMode.indexOf('toaxis') !== -1 || xMode.indexOf('across') !== -1) {
var yBase = yEdge;
var yEndSpike = yPoint;
if(xMode.indexOf('across') !== -1) {
yBase = xa._counterSpan[0];
yEndSpike = xa._counterSpan[1];
}
// Background vertical line (to x-axis)
container.append('line')
.attr({
'x1': xPoint,
'x2': xPoint,
'y1': yBase,
'y2': yEndSpike,
'stroke-width': xThickness + 2,
'stroke': contrastColor
})
.classed('spikeline', true)
.classed('crisp', true);
// Foreground vertical line (to x-axis)
container.append('line')
.attr({
'x1': xPoint,
'x2': xPoint,
'y1': yBase,
'y2': yEndSpike,
'stroke-width': xThickness,
'stroke': xColor,
'stroke-dasharray': Drawing.dashStyle(xa.spikedash, xThickness)
})
.classed('spikeline', true)
.classed('crisp', true);
}
// X axis marker
if(xMode.indexOf('marker') !== -1) {
container.append('circle')
.attr({
'cx': xPoint,
'cy': yEdge - (xa.side !== 'top' ? xThickness : -xThickness),
'r': xThickness,
'fill': xColor
})
.classed('spikeline', true);
}
}
}
function createHoverText(hoverData, opts) {
var hovermode = opts.hovermode,
rotateLabels = opts.rotateLabels,
bgColor = opts.bgColor,
container = opts.container,
outerContainer = opts.outerContainer,
// opts.fontFamily/Size are used for the common label
// and as defaults for each hover label, though the individual labels
// can override this.
fontFamily = opts.fontFamily || constants.HOVERFONT,
fontSize = opts.fontSize || constants.HOVERFONTSIZE,
c0 = hoverData[0],
xa = c0.xa,
ya = c0.ya,
commonAttr = hovermode === 'y' ? 'yLabel' : 'xLabel',
t0 = c0[commonAttr],
t00 = (String(t0) || '').split(' ')[0],
outerContainerBB = outerContainer.node().getBoundingClientRect(),
outerTop = outerContainerBB.top,
outerWidth = outerContainerBB.width,
outerHeight = outerContainerBB.height;
// show the common label, if any, on the axis
// never show a common label in array mode,
// even if sometimes there could be one
var showCommonLabel = c0.distance <= constants.MAXDIST &&
(hovermode === 'x' || hovermode === 'y');
// all hover traces hoverinfo must contain the hovermode
// to have common labels
var i, traceHoverinfo;
for(i = 0; i < hoverData.length; i++) {
traceHoverinfo = hoverData[i].trace.hoverinfo;
var parts = traceHoverinfo.split('+');
if(parts.indexOf('all') === -1 &&
parts.indexOf(hovermode) === -1) {
showCommonLabel = false;
break;
}
}
var commonLabel = container.selectAll('g.axistext')
.data(showCommonLabel ? [0] : []);
commonLabel.enter().append('g')
.classed('axistext', true);
commonLabel.exit().remove();
commonLabel.each(function() {
var label = d3.select(this),
lpath = label.selectAll('path').data([0]),
ltext = label.selectAll('text').data([0]);
lpath.enter().append('path')
.style({fill: Color.defaultLine, 'stroke-width': '1px', stroke: Color.background});
ltext.enter().append('text')
.call(Drawing.font, fontFamily, fontSize, Color.background)
// prohibit tex interpretation until we can handle
// tex and regular text together
.attr('data-notex', 1);
ltext.text(t0)
.call(svgTextUtils.convertToTspans)
.call(Drawing.setPosition, 0, 0)
.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
label.attr('transform', '');
var tbb = ltext.node().getBoundingClientRect();
if(hovermode === 'x') {
ltext.attr('text-anchor', 'middle')
.call(Drawing.setPosition, 0, (xa.side === 'top' ?
(outerTop - tbb.bottom - HOVERARROWSIZE - HOVERTEXTPAD) :
(outerTop - tbb.top + HOVERARROWSIZE + HOVERTEXTPAD)))
.selectAll('tspan.line')
.attr({
x: ltext.attr('x'),
y: ltext.attr('y')
});
var topsign = xa.side === 'top' ? '-' : '';
lpath.attr('d', 'M0,0' +
'L' + HOVERARROWSIZE + ',' + topsign + HOVERARROWSIZE +
'H' + (HOVERTEXTPAD + tbb.width / 2) +
'v' + topsign + (HOVERTEXTPAD * 2 + tbb.height) +
'H-' + (HOVERTEXTPAD + tbb.width / 2) +
'V' + topsign + HOVERARROWSIZE + 'H-' + HOVERARROWSIZE + 'Z');
label.attr('transform', 'translate(' +
(xa._offset + (c0.x0 + c0.x1) / 2) + ',' +
(ya._offset + (xa.side === 'top' ? 0 : ya._length)) + ')');
}
else {
ltext.attr('text-anchor', ya.side === 'right' ? 'start' : 'end')
.call(Drawing.setPosition,
(ya.side === 'right' ? 1 : -1) * (HOVERTEXTPAD + HOVERARROWSIZE),
outerTop - tbb.top - tbb.height / 2)
.selectAll('tspan.line')
.attr({
x: ltext.attr('x'),
y: ltext.attr('y')
});
var leftsign = ya.side === 'right' ? '' : '-';
lpath.attr('d', 'M0,0' +
'L' + leftsign + HOVERARROWSIZE + ',' + HOVERARROWSIZE +
'V' + (HOVERTEXTPAD + tbb.height / 2) +
'h' + leftsign + (HOVERTEXTPAD * 2 + tbb.width) +
'V-' + (HOVERTEXTPAD + tbb.height / 2) +
'H' + leftsign + HOVERARROWSIZE + 'V-' + HOVERARROWSIZE + 'Z');
label.attr('transform', 'translate(' +
(xa._offset + (ya.side === 'right' ? xa._length : 0)) + ',' +
(ya._offset + (c0.y0 + c0.y1) / 2) + ')');
}
// remove the "close but not quite" points
// because of error bars, only take up to a space
hoverData = hoverData.filter(function(d) {
return (d.zLabelVal !== undefined) ||
(d[commonAttr] || '').split(' ')[0] === t00;
});
});
// show all the individual labels
// first create the objects
var hoverLabels = container.selectAll('g.hovertext')
.data(hoverData, function(d) {
return [d.trace.index, d.index, d.x0, d.y0, d.name, d.attr, d.xa, d.ya || ''].join(',');
});
hoverLabels.enter().append('g')
.classed('hovertext', true)
.each(function() {
var g = d3.select(this);
// trace name label (rect and text.name)
g.append('rect')
.call(Color.fill, Color.addOpacity(bgColor, 0.8));
g.append('text').classed('name', true);
// trace data label (path and text.nums)
g.append('path')
.style('stroke-width', '1px');
g.append('text').classed('nums', true)
.call(Drawing.font, fontFamily, fontSize);
});
hoverLabels.exit().remove();
// then put the text in, position the pointer to the data,
// and figure out sizes
hoverLabels.each(function(d) {
var g = d3.select(this).attr('transform', ''),
name = '',
text = '',
// combine possible non-opaque trace color with bgColor
baseColor = Color.opacity(d.color) ?
d.color : Color.defaultLine,
traceColor = Color.combine(baseColor, bgColor),
// find a contrasting color for border and text
contrastColor = d.borderColor || Color.contrast(traceColor);
// to get custom 'name' labels pass cleanPoint
if(d.nameOverride !== undefined) d.name = d.nameOverride;
if(d.name && d.zLabelVal === undefined) {
// strip out our pseudo-html elements from d.name (if it exists at all)
name = svgTextUtils.plainText(d.name || '');
if(name.length > 15) name = name.substr(0, 12) + '...';
}
// used by other modules (initially just ternary) that
// manage their own hoverinfo independent of cleanPoint
// the rest of this will still apply, so such modules
// can still put things in (x|y|z)Label, text, and name
// and hoverinfo will still determine their visibility
if(d.extraText !== undefined) text += d.extraText;
if(d.zLabel !== undefined) {
if(d.xLabel !== undefined) text += 'x: ' + d.xLabel + '<br>';
if(d.yLabel !== undefined) text += 'y: ' + d.yLabel + '<br>';
text += (text ? 'z: ' : '') + d.zLabel;
}
else if(showCommonLabel && d[hovermode + 'Label'] === t0) {
text = d[(hovermode === 'x' ? 'y' : 'x') + 'Label'] || '';
}
else if(d.xLabel === undefined) {
if(d.yLabel !== undefined) text = d.yLabel;
}
else if(d.yLabel === undefined) text = d.xLabel;
else text = '(' + d.xLabel + ', ' + d.yLabel + ')';
if(d.text && !Array.isArray(d.text)) text += (text ? '<br>' : '') + d.text;
// if 'text' is empty at this point,
// put 'name' in main label and don't show secondary label
if(text === '') {
// if 'name' is also empty, remove entire label
if(name === '') g.remove();
text = name;
}
// main label
var tx = g.select('text.nums')
.call(Drawing.font,
d.fontFamily || fontFamily,
d.fontSize || fontSize,
d.fontColor || contrastColor)
.call(Drawing.setPosition, 0, 0)
.text(text)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans);
tx.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
var tx2 = g.select('text.name'),
tx2width = 0;
// secondary label for non-empty 'name'
if(name && name !== text) {
tx2.call(Drawing.font,
d.fontFamily || fontFamily,
d.fontSize || fontSize,
traceColor)
.text(name)
.call(Drawing.setPosition, 0, 0)
.attr('data-notex', 1)
.call(svgTextUtils.convertToTspans);
tx2.selectAll('tspan.line')
.call(Drawing.setPosition, 0, 0);
tx2width = tx2.node().getBoundingClientRect().width + 2 * HOVERTEXTPAD;
}
else {
tx2.remove();
g.select('rect').remove();
}
g.select('path')
.style({
fill: traceColor,
stroke: contrastColor
});
var tbb = tx.node().getBoundingClientRect(),
htx = d.xa._offset + (d.x0 + d.x1) / 2,
hty = d.ya._offset + (d.y0 + d.y1) / 2,
dx = Math.abs(d.x1 - d.x0),
dy = Math.abs(d.y1 - d.y0),
txTotalWidth = tbb.width + HOVERARROWSIZE + HOVERTEXTPAD + tx2width,
anchorStartOK,
anchorEndOK;
d.ty0 = outerTop - tbb.top;
d.bx = tbb.width + 2 * HOVERTEXTPAD;
d.by = tbb.height + 2 * HOVERTEXTPAD;
d.anchor = 'start';
d.txwidth = tbb.width;
d.tx2width = tx2width;
d.offset = 0;
if(rotateLabels) {
d.pos = htx;
anchorStartOK = hty + dy / 2 + txTotalWidth <= outerHeight;
anchorEndOK = hty - dy / 2 - txTotalWidth >= 0;
if((d.idealAlign === 'top' || !anchorStartOK) && anchorEndOK) {
hty -= dy / 2;
d.anchor = 'end';
} else if(anchorStartOK) {
hty += dy / 2;
d.anchor = 'start';
} else d.anchor = 'middle';
}
else {
d.pos = hty;
anchorStartOK = htx + dx / 2 + txTotalWidth <= outerWidth;
anchorEndOK = htx - dx / 2 - txTotalWidth >= 0;
if((d.idealAlign === 'left' || !anchorStartOK) && anchorEndOK) {
htx -= dx / 2;
d.anchor = 'end';
} else if(anchorStartOK) {
htx += dx / 2;
d.anchor = 'start';
} else d.anchor = 'middle';
}
tx.attr('text-anchor', d.anchor);
if(tx2width) tx2.attr('text-anchor', d.anchor);
g.attr('transform', 'translate(' + htx + ',' + hty + ')' +
(rotateLabels ? 'rotate(' + YANGLE + ')' : ''));
});
return hoverLabels;
}
// Make groups of touching points, and within each group
// move each point so that no labels overlap, but the average
// label position is the same as it was before moving. Indicentally,
// this is equivalent to saying all the labels are on equal linear
// springs about their initial position. Initially, each point is
// its own group, but as we find overlaps we will clump the points.
//
// Also, there are hard constraints at the edges of the graphs,
// that push all groups to the middle so they are visible. I don't
// know what happens if the group spans all the way from one edge to
// the other, though it hardly matters - there's just too much
// information then.
function hoverAvoidOverlaps(hoverData, ax) {
var nummoves = 0,
// make groups of touching points
pointgroups = hoverData
.map(function(d, i) {
var axis = d[ax];
return [{
i: i,
dp: 0,
pos: d.pos,
posref: d.posref,
size: d.by * (axis._id.charAt(0) === 'x' ? YFACTOR : 1) / 2,
pmin: axis._offset,
pmax: axis._offset + axis._length
}];
})
.sort(function(a, b) { return a[0].posref - b[0].posref; }),
donepositioning,
topOverlap,
bottomOverlap,
i, j,
pti,
sumdp;
function constrainGroup(grp) {
var minPt = grp[0],
maxPt = grp[grp.length - 1];
// overlap with the top - positive vals are overlaps
topOverlap = minPt.pmin - minPt.pos - minPt.dp + minPt.size;
// overlap with the bottom - positive vals are overlaps
bottomOverlap = maxPt.pos + maxPt.dp + maxPt.size - minPt.pmax;
// check for min overlap first, so that we always
// see the largest labels
// allow for .01px overlap, so we don't get an
// infinite loop from rounding errors
if(topOverlap > 0.01) {
for(j = grp.length - 1; j >= 0; j--) grp[j].dp += topOverlap;
donepositioning = false;
}
if(bottomOverlap < 0.01) return;
if(topOverlap < -0.01) {
// make sure we're not pushing back and forth
for(j = grp.length - 1; j >= 0; j--) grp[j].dp -= bottomOverlap;
donepositioning = false;
}
if(!donepositioning) return;
// no room to fix positioning, delete off-screen points
// first see how many points we need to delete
var deleteCount = 0;
for(i = 0; i < grp.length; i++) {
pti = grp[i];
if(pti.pos + pti.dp + pti.size > minPt.pmax) deleteCount++;
}
// start by deleting points whose data is off screen
for(i = grp.length - 1; i >= 0; i--) {
if(deleteCount <= 0) break;
pti = grp[i];
// pos has already been constrained to [pmin,pmax]
// so look for points close to that to delete
if(pti.pos > minPt.pmax - 1) {
pti.del = true;
deleteCount--;
}
}
for(i = 0; i < grp.length; i++) {
if(deleteCount <= 0) break;
pti = grp[i];
// pos has already been constrained to [pmin,pmax]
// so look for points close to that to delete
if(pti.pos < minPt.pmin + 1) {
pti.del = true;
deleteCount--;
// shift the whole group minus into this new space
bottomOverlap = pti.size * 2;
for(j = grp.length - 1; j >= 0; j--) grp[j].dp -= bottomOverlap;
}
}
// then delete points that go off the bottom
for(i = grp.length - 1; i >= 0; i--) {
if(deleteCount <= 0) break;
pti = grp[i];
if(pti.pos + pti.dp + pti.size > minPt.pmax) {
pti.del = true;
deleteCount--;
}
}
}
// loop through groups, combining them if they overlap,
// until nothing moves
while(!donepositioning && nummoves <= hoverData.length) {
// to avoid infinite loops, don't move more times
// than there are traces
nummoves++;
// assume nothing will move in this iteration,
// reverse this if it does
donepositioning = true;
i = 0;
while(i < pointgroups.length - 1) {
// the higher (g0) and lower (g1) point group
var g0 = pointgroups[i],
g1 = pointgroups[i + 1],
// the lowest point in the higher group (p0)
// the highest point in the lower group (p1)
p0 = g0[g0.length - 1],
p1 = g1[0];
topOverlap = p0.pos + p0.dp + p0.size - p1.pos - p1.dp + p1.size;
// Only group points that lie on the same axes
if(topOverlap > 0.01 && (p0.pmin === p1.pmin) && (p0.pmax === p1.pmax)) {
// push the new point(s) added to this group out of the way
for(j = g1.length - 1; j >= 0; j--) g1[j].dp += topOverlap;
// add them to the group
g0.push.apply(g0, g1);
pointgroups.splice(i + 1, 1);
// adjust for minimum average movement
sumdp = 0;
for(j = g0.length - 1; j >= 0; j--) sumdp += g0[j].dp;
bottomOverlap = sumdp / g0.length;
for(j = g0.length - 1; j >= 0; j--) g0[j].dp -= bottomOverlap;
donepositioning = false;
}
else i++;
}
// check if we're going off the plot on either side and fix
pointgroups.forEach(constrainGroup);
}
// now put these offsets into hoverData
for(i = pointgroups.length - 1; i >= 0; i--) {
var grp = pointgroups[i];
for(j = grp.length - 1; j >= 0; j--) {
var pt = grp[j],
hoverPt = hoverData[pt.i];
hoverPt.offset = pt.dp;
hoverPt.del = pt.del;
}
}
}
function alignHoverText(hoverLabels, rotateLabels) {
// finally set the text positioning relative to the data and draw the
// box around it
hoverLabels.each(function(d) {
var g = d3.select(this);
if(d.del) {
g.remove();
return;
}
var horzSign = d.anchor === 'end' ? -1 : 1,
tx = g.select('text.nums'),
alignShift = {start: 1, end: -1, middle: 0}[d.anchor],
txx = alignShift * (HOVERARROWSIZE + HOVERTEXTPAD),
tx2x = txx + alignShift * (d.txwidth + HOVERTEXTPAD),
offsetX = 0,
offsetY = d.offset;
if(d.anchor === 'middle') {
txx -= d.tx2width / 2;
tx2x -= d.tx2width / 2;
}
if(rotateLabels) {
offsetY *= -YSHIFTY;
offsetX = d.offset * YSHIFTX;
}
g.select('path').attr('d', d.anchor === 'middle' ?
// middle aligned: rect centered on data
('M-' + (d.bx / 2) + ',-' + (d.by / 2) + 'h' + d.bx + 'v' + d.by + 'h-' + d.bx + 'Z') :
// left or right aligned: side rect with arrow to data
('M0,0L' + (horzSign * HOVERARROWSIZE + offsetX) + ',' + (HOVERARROWSIZE + offsetY) +
'v' + (d.by / 2 - HOVERARROWSIZE) +
'h' + (horzSign * d.bx) +
'v-' + d.by +
'H' + (horzSign * HOVERARROWSIZE + offsetX) +
'V' + (offsetY - HOVERARROWSIZE) +
'Z'));
tx.call(Drawing.setPosition,
txx + offsetX, offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD)
.selectAll('tspan.line')
.attr({
x: tx.attr('x'),
y: tx.attr('y')
});
if(d.tx2width) {
g.select('text.name, text.name tspan.line')
.call(Drawing.setPosition,
tx2x + alignShift * HOVERTEXTPAD + offsetX,
offsetY + d.ty0 - d.by / 2 + HOVERTEXTPAD);
g.select('rect')
.call(Drawing.setRect,
tx2x + (alignShift - 1) * d.tx2width / 2 + offsetX,
offsetY - d.by / 2 - 1,
d.tx2width, d.by + 2);
}
});
}
function hoverChanged(gd, evt, oldhoverdata) {
// don't emit any events if nothing changed
if(!oldhoverdata || oldhoverdata.length !== gd._hoverdata.length) return true;
for(var i = oldhoverdata.length - 1; i >= 0; i--) {
var oldPt = oldhoverdata[i],
newPt = gd._hoverdata[i];
if(oldPt.curveNumber !== newPt.curveNumber ||
String(oldPt.pointNumber) !== String(newPt.pointNumber)) {
return true;
}
}
return false;
}
// on click
fx.click = function(gd, evt) {
var annotationsDone = Registry.getComponentMethod('annotations', 'onClick')(gd, gd._hoverdata);
function emitClick() { gd.emit('plotly_click', {points: gd._hoverdata, event: evt}); }
if(gd._hoverdata && evt && evt.target) {
if(annotationsDone && annotationsDone.then) {
annotationsDone.then(emitClick);
}
else emitClick();
// why do we get a double event without this???
if(evt.stopImmediatePropagation) evt.stopImmediatePropagation();
}
};
// for bar charts and others with finite-size objects: you must be inside
// it to see its hover info, so distance is infinite outside.
// But make distance inside be at least 1/4 MAXDIST, and a little bigger
// for bigger bars, to prioritize scatter and smaller bars over big bars
// note that for closest mode, two inbox's will get added in quadrature
// args are (signed) difference from the two opposite edges
// count one edge as in, so that over continuous ranges you never get a gap
fx.inbox = function(v0, v1) {
if(v0 * v1 < 0 || v0 === 0) {
return constants.MAXDIST * (0.6 - 0.3 / Math.max(3, Math.abs(v0 - v1)));
}
return Infinity;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Lib = require('../../lib');
var Plots = require('../plots');
var axisIds = require('./axis_ids');
var constants = require('./constants');
exports.name = 'cartesian';
exports.attr = ['xaxis', 'yaxis'];
exports.idRoot = ['x', 'y'];
exports.idRegex = constants.idRegex;
exports.attrRegex = constants.attrRegex;
exports.attributes = require('./attributes');
exports.layoutAttributes = require('./layout_attributes');
exports.transitionAxes = require('./transition_axes');
exports.plot = function(gd, traces, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout,
subplots = Plots.getSubplotIds(fullLayout, 'cartesian'),
calcdata = gd.calcdata,
i;
// If traces is not provided, then it's a complete replot and missing
// traces are removed
if(!Array.isArray(traces)) {
traces = [];
for(i = 0; i < calcdata.length; i++) {
traces.push(i);
}
}
for(i = 0; i < subplots.length; i++) {
var subplot = subplots[i],
subplotInfo = fullLayout._plots[subplot];
// Get all calcdata for this subplot:
var cdSubplot = [];
var pcd;
for(var j = 0; j < calcdata.length; j++) {
var cd = calcdata[j],
trace = cd[0].trace;
// Skip trace if whitelist provided and it's not whitelisted:
// if (Array.isArray(traces) && traces.indexOf(i) === -1) continue;
if(trace.xaxis + trace.yaxis === subplot) {
// XXX: Should trace carpet dependencies. Only replot all carpet plots if the carpet
// axis has actually changed:
//
// If this trace is specifically requested, add it to the list:
if(traces.indexOf(trace.index) !== -1 || trace.carpet) {
// Okay, so example: traces 0, 1, and 2 have fill = tonext. You animate
// traces 0 and 2. Trace 1 also needs to be updated, otherwise its fill
// is outdated. So this retroactively adds the previous trace if the
// traces are interdependent.
if(
pcd &&
pcd[0].trace.xaxis + pcd[0].trace.yaxis === subplot &&
['tonextx', 'tonexty', 'tonext'].indexOf(trace.fill) !== -1 &&
cdSubplot.indexOf(pcd) === -1
) {
cdSubplot.push(pcd);
}
cdSubplot.push(cd);
}
// Track the previous trace on this subplot for the retroactive-add step
// above:
pcd = cd;
}
}
plotOne(gd, subplotInfo, cdSubplot, transitionOpts, makeOnCompleteCallback);
}
};
function plotOne(gd, plotinfo, cdSubplot, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout,
modules = fullLayout._modules;
// remove old traces, then redraw everything
//
// TODO: scatterlayer is manually excluded from this since it knows how
// to update instead of fully removing and redrawing every time. The
// remaining plot traces should also be able to do this. Once implemented,
// we won't need this - which should sometimes be a big speedup.
if(plotinfo.plot) {
plotinfo.plot.selectAll('g:not(.scatterlayer)').selectAll('g.trace').remove();
}
// plot all traces for each module at once
for(var j = 0; j < modules.length; j++) {
var _module = modules[j];
// skip over non-cartesian trace modules
if(_module.basePlotModule.name !== 'cartesian') continue;
// plot all traces of this type on this subplot at once
var cdModule = [];
for(var k = 0; k < cdSubplot.length; k++) {
var cd = cdSubplot[k],
trace = cd[0].trace;
if((trace._module === _module) && (trace.visible === true)) {
cdModule.push(cd);
}
}
_module.plot(gd, plotinfo, cdModule, transitionOpts, makeOnCompleteCallback);
}
}
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldModules = oldFullLayout._modules || [],
newModules = newFullLayout._modules || [];
var hadScatter, hasScatter, i;
for(i = 0; i < oldModules.length; i++) {
if(oldModules[i].name === 'scatter') {
hadScatter = true;
break;
}
}
for(i = 0; i < newModules.length; i++) {
if(newModules[i].name === 'scatter') {
hasScatter = true;
break;
}
}
if(hadScatter && !hasScatter) {
var oldPlots = oldFullLayout._plots,
ids = Object.keys(oldPlots || {});
for(i = 0; i < ids.length; i++) {
var subplotInfo = oldPlots[ids[i]];
if(subplotInfo.plot) {
subplotInfo.plot.select('g.scatterlayer')
.selectAll('g.trace')
.remove();
}
}
oldFullLayout._infolayer.selectAll('g.rangeslider-container')
.select('g.scatterlayer')
.selectAll('g.trace')
.remove();
}
var hadCartesian = (oldFullLayout._has && oldFullLayout._has('cartesian'));
var hasCartesian = (newFullLayout._has && newFullLayout._has('cartesian'));
if(hadCartesian && !hasCartesian) {
var subplotLayers = oldFullLayout._cartesianlayer.selectAll('.subplot');
var axIds = axisIds.listIds({ _fullLayout: oldFullLayout });
subplotLayers.call(purgeSubplotLayers, oldFullLayout);
oldFullLayout._defs.selectAll('.axesclip').remove();
for(i = 0; i < axIds.length; i++) {
oldFullLayout._infolayer.select('.' + axIds[i] + 'title').remove();
}
}
};
exports.drawFramework = function(gd) {
var fullLayout = gd._fullLayout,
subplotData = makeSubplotData(gd);
var subplotLayers = fullLayout._cartesianlayer.selectAll('.subplot')
.data(subplotData, Lib.identity);
subplotLayers.enter().append('g')
.attr('class', function(name) { return 'subplot ' + name; });
subplotLayers.order();
subplotLayers.exit()
.call(purgeSubplotLayers, fullLayout);
subplotLayers.each(function(name) {
var plotinfo = fullLayout._plots[name];
// keep ref to plot group
plotinfo.plotgroup = d3.select(this);
// initialize list of overlay subplots
plotinfo.overlays = [];
makeSubplotLayer(plotinfo);
// fill in list of overlay subplots
if(plotinfo.mainplot) {
var mainplot = fullLayout._plots[plotinfo.mainplot];
mainplot.overlays.push(plotinfo);
}
// make separate drag layers for each subplot,
// but append them to paper rather than the plot groups,
// so they end up on top of the rest
plotinfo.draglayer = joinLayer(fullLayout._draggers, 'g', name);
});
};
exports.rangePlot = function(gd, plotinfo, cdSubplot) {
makeSubplotLayer(plotinfo);
plotOne(gd, plotinfo, cdSubplot);
Plots.style(gd);
};
function makeSubplotData(gd) {
var fullLayout = gd._fullLayout,
subplots = Object.keys(fullLayout._plots);
var subplotData = [],
overlays = [];
for(var i = 0; i < subplots.length; i++) {
var subplot = subplots[i],
plotinfo = fullLayout._plots[subplot];
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
// is this subplot overlaid on another?
// ax.overlaying is the id of another axis of the same
// dimension that this one overlays to be an overlaid subplot,
// the main plot must exist make sure we're not trying to
// overlay on an axis that's already overlaying another
var xa2 = axisIds.getFromId(gd, xa.overlaying) || xa;
if(xa2 !== xa && xa2.overlaying) {
xa2 = xa;
xa.overlaying = false;
}
var ya2 = axisIds.getFromId(gd, ya.overlaying) || ya;
if(ya2 !== ya && ya2.overlaying) {
ya2 = ya;
ya.overlaying = false;
}
var mainplot = xa2._id + ya2._id;
if(mainplot !== subplot && subplots.indexOf(mainplot) !== -1) {
plotinfo.mainplot = mainplot;
plotinfo.mainplotinfo = fullLayout._plots[mainplot];
overlays.push(subplot);
// for now force overlays to overlay completely... so they
// can drag together correctly and share backgrounds.
// Later perhaps we make separate axis domain and
// tick/line domain or something, so they can still share
// the (possibly larger) dragger and background but don't
// have to both be drawn over that whole domain
xa.domain = xa2.domain.slice();
ya.domain = ya2.domain.slice();
}
else {
subplotData.push(subplot);
}
}
// main subplots before overlays
subplotData = subplotData.concat(overlays);
return subplotData;
}
function makeSubplotLayer(plotinfo) {
var plotgroup = plotinfo.plotgroup,
id = plotinfo.id;
// Layers to keep plot types in the right order.
// from back to front:
// 1. heatmaps, 2D histos and contour maps
// 2. bars / 1D histos
// 3. errorbars for bars and scatter
// 4. scatter
// 5. box plots
function joinPlotLayers(parent) {
joinLayer(parent, 'g', 'imagelayer');
joinLayer(parent, 'g', 'maplayer');
joinLayer(parent, 'g', 'barlayer');
joinLayer(parent, 'g', 'carpetlayer');
joinLayer(parent, 'g', 'boxlayer');
joinLayer(parent, 'g', 'scatterlayer');
}
if(!plotinfo.mainplot) {
var backLayer = joinLayer(plotgroup, 'g', 'layer-subplot');
plotinfo.shapelayer = joinLayer(backLayer, 'g', 'shapelayer');
plotinfo.imagelayer = joinLayer(backLayer, 'g', 'imagelayer');
plotinfo.gridlayer = joinLayer(plotgroup, 'g', 'gridlayer');
plotinfo.overgrid = joinLayer(plotgroup, 'g', 'overgrid');
plotinfo.zerolinelayer = joinLayer(plotgroup, 'g', 'zerolinelayer');
plotinfo.overzero = joinLayer(plotgroup, 'g', 'overzero');
plotinfo.plot = joinLayer(plotgroup, 'g', 'plot');
plotinfo.overplot = joinLayer(plotgroup, 'g', 'overplot');
plotinfo.xlines = joinLayer(plotgroup, 'path', 'xlines');
plotinfo.ylines = joinLayer(plotgroup, 'path', 'ylines');
plotinfo.overlines = joinLayer(plotgroup, 'g', 'overlines');
plotinfo.xaxislayer = joinLayer(plotgroup, 'g', 'xaxislayer');
plotinfo.yaxislayer = joinLayer(plotgroup, 'g', 'yaxislayer');
plotinfo.overaxes = joinLayer(plotgroup, 'g', 'overaxes');
}
else {
var mainplotinfo = plotinfo.mainplotinfo;
// now make the components of overlaid subplots
// overlays don't have backgrounds, and append all
// their other components to the corresponding
// extra groups of their main plots.
plotinfo.gridlayer = joinLayer(mainplotinfo.overgrid, 'g', id);
plotinfo.zerolinelayer = joinLayer(mainplotinfo.overzero, 'g', id);
plotinfo.plot = joinLayer(mainplotinfo.overplot, 'g', id);
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
}
// common attributes for all subplots, overlays or not
plotinfo.plot.call(joinPlotLayers);
plotinfo.xlines
.style('fill', 'none')
.classed('crisp', true);
plotinfo.ylines
.style('fill', 'none')
.classed('crisp', true);
}
function purgeSubplotLayers(layers, fullLayout) {
if(!layers) return;
layers.each(function(subplot) {
var plotgroup = d3.select(this),
clipId = 'clip' + fullLayout._uid + subplot + 'plot';
plotgroup.remove();
fullLayout._draggers.selectAll('g.' + subplot).remove();
fullLayout._defs.select('#' + clipId).remove();
// do not remove individual axis <clipPath>s here
// as other subplots may need them
});
}
function joinLayer(parent, nodeType, className) {
var layer = parent.selectAll('.' + className)
.data([0]);
layer.enter().append(nodeType)
.classed(className, true);
return layer;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var fontAttrs = require('../font_attributes');
var colorAttrs = require('../../components/color/attributes');
var dash = require('../../components/drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
var constants = require('./constants');
module.exports = {
visible: {
valType: 'boolean',
role: 'info',
description: [
'A single toggle to hide the axis while preserving interaction like dragging.',
'Default is true when a cheater plot is present on the axis, otherwise',
'false'
].join(' ')
},
color: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: [
'Sets default for all colors associated with this axis',
'all at once: line, font, tick, and grid colors.',
'Grid color is lightened by blending this with the plot background',
'Individual pieces can override this.'
].join(' ')
},
title: {
valType: 'string',
role: 'info',
description: 'Sets the title of this axis.'
},
titlefont: extendFlat({}, fontAttrs, {
description: [
'Sets this axis\' title font.'
].join(' ')
}),
type: {
valType: 'enumerated',
// '-' means we haven't yet run autotype or couldn't find any data
// it gets turned into linear in gd._fullLayout but not copied back
// to gd.data like the others are.
values: ['-', 'linear', 'log', 'date', 'category'],
dflt: '-',
role: 'info',
description: [
'Sets the axis type.',
'By default, plotly attempts to determined the axis type',
'by looking into the data of the traces that referenced',
'the axis in question.'
].join(' ')
},
autorange: {
valType: 'enumerated',
values: [true, false, 'reversed'],
dflt: true,
role: 'style',
description: [
'Determines whether or not the range of this axis is',
'computed in relation to the input data.',
'See `rangemode` for more info.',
'If `range` is provided, then `autorange` is set to *false*.'
].join(' ')
},
rangemode: {
valType: 'enumerated',
values: ['normal', 'tozero', 'nonnegative'],
dflt: 'normal',
role: 'style',
description: [
'If *normal*, the range is computed in relation to the extrema',
'of the input data.',
'If *tozero*`, the range extends to 0,',
'regardless of the input data',
'If *nonnegative*, the range is non-negative,',
'regardless of the input data.'
].join(' ')
},
range: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'any'},
{valType: 'any'}
],
description: [
'Sets the range of this axis.',
'If the axis `type` is *log*, then you must take the log of your',
'desired range (e.g. to set the range from 1 to 100,',
'set the range from 0 to 2).',
'If the axis `type` is *date*, it should be date strings,',
'like date data, though Date objects and unix milliseconds',
'will be accepted and converted to strings.',
'If the axis `type` is *category*, it should be numbers,',
'using the scale where each category is assigned a serial',
'number from zero in the order it appears.'
].join(' ')
},
fixedrange: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'Determines whether or not this axis is zoom-able.',
'If true, then zoom is disabled.'
].join(' ')
},
// scaleanchor: not used directly, just put here for reference
// values are any opposite-letter axis id
scaleanchor: {
valType: 'enumerated',
values: [
constants.idRegex.x.toString(),
constants.idRegex.y.toString()
],
role: 'info',
description: [
'If set to an opposite-letter axis id (e.g. `x2`, `y`), the range of this axis',
'changes together with the range of the corresponding opposite-letter axis.',
'such that the scale of pixels per unit is in a constant ratio.',
'Both axes are still zoomable, but when you zoom one, the other will',
'zoom the same amount, keeping a fixed midpoint.',
'Autorange will also expand about the midpoints to satisfy the constraint.',
'You can chain these, ie `yaxis: {scaleanchor: *x*}, xaxis2: {scaleanchor: *y*}`',
'but you can only link axes of the same `type`.',
'Loops (`yaxis: {scaleanchor: *x*}, xaxis: {scaleanchor: *y*}` or longer) are redundant',
'and the last constraint encountered will be ignored to avoid possible',
'inconsistent constraints via `scaleratio`.'
].join(' ')
},
scaleratio: {
valType: 'number',
min: 0,
dflt: 1,
role: 'info',
description: [
'If this axis is linked to another by `scaleanchor`, this determines the pixel',
'to unit scale ratio. For example, if this value is 10, then every unit on',
'this axis spans 10 times the number of pixels as a unit on the linked axis.',
'Use this for example to create an elevation profile where the vertical scale',
'is exaggerated a fixed amount with respect to the horizontal.'
].join(' ')
},
// ticks
tickmode: {
valType: 'enumerated',
values: ['auto', 'linear', 'array'],
role: 'info',
description: [
'Sets the tick mode for this axis.',
'If *auto*, the number of ticks is set via `nticks`.',
'If *linear*, the placement of the ticks is determined by',
'a starting position `tick0` and a tick step `dtick`',
'(*linear* is the default value if `tick0` and `dtick` are provided).',
'If *array*, the placement of the ticks is set via `tickvals`',
'and the tick text is `ticktext`.',
'(*array* is the default value if `tickvals` is provided).'
].join(' ')
},
nticks: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'style',
description: [
'Specifies the maximum number of ticks for the particular axis.',
'The actual number of ticks will be chosen automatically to be',
'less than or equal to `nticks`.',
'Has an effect only if `tickmode` is set to *auto*.'
].join(' ')
},
tick0: {
valType: 'any',
role: 'style',
description: [
'Sets the placement of the first tick on this axis.',
'Use with `dtick`.',
'If the axis `type` is *log*, then you must take the log of your starting tick',
'(e.g. to set the starting tick to 100, set the `tick0` to 2)',
'except when `dtick`=*L<f>* (see `dtick` for more info).',
'If the axis `type` is *date*, it should be a date string, like date data.',
'If the axis `type` is *category*, it should be a number, using the scale where',
'each category is assigned a serial number from zero in the order it appears.'
].join(' ')
},
dtick: {
valType: 'any',
role: 'style',
description: [
'Sets the step in-between ticks on this axis. Use with `tick0`.',
'Must be a positive number, or special strings available to *log* and *date* axes.',
'If the axis `type` is *log*, then ticks are set every 10^(n*dtick) where n',
'is the tick number. For example,',
'to set a tick mark at 1, 10, 100, 1000, ... set dtick to 1.',
'To set tick marks at 1, 100, 10000, ... set dtick to 2.',
'To set tick marks at 1, 5, 25, 125, 625, 3125, ... set dtick to log_10(5), or 0.69897000433.',
'*log* has several special values; *L<f>*, where `f` is a positive number,',
'gives ticks linearly spaced in value (but not position).',
'For example `tick0` = 0.1, `dtick` = *L0.5* will put ticks at 0.1, 0.6, 1.1, 1.6 etc.',
'To show powers of 10 plus small digits between, use *D1* (all digits) or *D2* (only 2 and 5).',
'`tick0` is ignored for *D1* and *D2*.',
'If the axis `type` is *date*, then you must convert the time to milliseconds.',
'For example, to set the interval between ticks to one day,',
'set `dtick` to 86400000.0.',
'*date* also has special values *M<n>* gives ticks spaced by a number of months.',
'`n` must be a positive integer.',
'To set ticks on the 15th of every third month, set `tick0` to *2000-01-15* and `dtick` to *M3*.',
'To set ticks every 4 years, set `dtick` to *M48*'
].join(' ')
},
tickvals: {
valType: 'data_array',
description: [
'Sets the values at which ticks on this axis appear.',
'Only has an effect if `tickmode` is set to *array*.',
'Used with `ticktext`.'
].join(' ')
},
ticktext: {
valType: 'data_array',
description: [
'Sets the text displayed at the ticks position via `tickvals`.',
'Only has an effect if `tickmode` is set to *array*.',
'Used with `tickvals`.'
].join(' ')
},
ticks: {
valType: 'enumerated',
values: ['outside', 'inside', ''],
role: 'style',
description: [
'Determines whether ticks are drawn or not.',
'If **, this axis\' ticks are not drawn.',
'If *outside* (*inside*), this axis\' are drawn outside (inside)',
'the axis lines.'
].join(' ')
},
mirror: {
valType: 'enumerated',
values: [true, 'ticks', false, 'all', 'allticks'],
dflt: false,
role: 'style',
description: [
'Determines if the axis lines or/and ticks are mirrored to',
'the opposite side of the plotting area.',
'If *true*, the axis lines are mirrored.',
'If *ticks*, the axis lines and ticks are mirrored.',
'If *false*, mirroring is disable.',
'If *all*, axis lines are mirrored on all shared-axes subplots.',
'If *allticks*, axis lines and ticks are mirrored',
'on all shared-axes subplots.'
].join(' ')
},
ticklen: {
valType: 'number',
min: 0,
dflt: 5,
role: 'style',
description: 'Sets the tick length (in px).'
},
tickwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the tick width (in px).'
},
tickcolor: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: 'Sets the tick color.'
},
showticklabels: {
valType: 'boolean',
dflt: true,
role: 'style',
description: 'Determines whether or not the tick labels are drawn.'
},
showspikes: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'Determines whether or not spikes (aka droplines) are drawn for this axis.',
'Note: This only takes affect when hovermode = closest'
].join(' ')
},
spikecolor: {
valType: 'color',
dflt: null,
role: 'style',
description: 'Sets the spike color. If undefined, will use the series color'
},
spikethickness: {
valType: 'number',
dflt: 3,
role: 'style',
description: 'Sets the width (in px) of the zero line.'
},
spikedash: extendFlat({}, dash, {dflt: 'dash'}),
spikemode: {
valType: 'flaglist',
flags: ['toaxis', 'across', 'marker'],
role: 'style',
dflt: 'toaxis',
description: [
'Determines the drawing mode for the spike line',
'If *toaxis*, the line is drawn from the data point to the axis the ',
'series is plotted on.',
'If *across*, the line is drawn across the entire plot area, and',
'supercedes *toaxis*.',
'If *marker*, then a marker dot is drawn on the axis the series is',
'plotted on'
].join(' ')
},
tickfont: extendFlat({}, fontAttrs, {
description: 'Sets the tick font.'
}),
tickangle: {
valType: 'angle',
dflt: 'auto',
role: 'style',
description: [
'Sets the angle of the tick labels with respect to the horizontal.',
'For example, a `tickangle` of -90 draws the tick labels',
'vertically.'
].join(' ')
},
tickprefix: {
valType: 'string',
dflt: '',
role: 'style',
description: 'Sets a tick label prefix.'
},
showtickprefix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: [
'If *all*, all tick labels are displayed with a prefix.',
'If *first*, only the first tick is displayed with a prefix.',
'If *last*, only the last tick is displayed with a suffix.',
'If *none*, tick prefixes are hidden.'
].join(' ')
},
ticksuffix: {
valType: 'string',
dflt: '',
role: 'style',
description: 'Sets a tick label suffix.'
},
showticksuffix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: 'Same as `showtickprefix` but for tick suffixes.'
},
showexponent: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: [
'If *all*, all exponents are shown besides their significands.',
'If *first*, only the exponent of the first tick is shown.',
'If *last*, only the exponent of the last tick is shown.',
'If *none*, no exponents appear.'
].join(' ')
},
exponentformat: {
valType: 'enumerated',
values: ['none', 'e', 'E', 'power', 'SI', 'B'],
dflt: 'B',
role: 'style',
description: [
'Determines a formatting rule for the tick exponents.',
'For example, consider the number 1,000,000,000.',
'If *none*, it appears as 1,000,000,000.',
'If *e*, 1e+9.',
'If *E*, 1E+9.',
'If *power*, 1x10^9 (with 9 in a super script).',
'If *SI*, 1G.',
'If *B*, 1B.'
].join(' ')
},
separatethousands: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'If "true", even 4-digit integers are separated'
].join(' ')
},
tickformat: {
valType: 'string',
dflt: '',
role: 'style',
description: [
'Sets the tick label formatting rule using d3 formatting mini-languages',
'which are very similar to those in Python. For numbers, see:',
'https://github.com/d3/d3-format/blob/master/README.md#locale_format',
'And for dates see:',
'https://github.com/d3/d3-time-format/blob/master/README.md#locale_format',
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
'*%H~%M~%S.%2f* would display *09~15~23.46*'
].join(' ')
},
hoverformat: {
valType: 'string',
dflt: '',
role: 'style',
description: [
'Sets the hover text formatting rule using d3 formatting mini-languages',
'which are very similar to those in Python. For numbers, see:',
'https://github.com/d3/d3-format/blob/master/README.md#locale_format',
'And for dates see:',
'https://github.com/d3/d3-time-format/blob/master/README.md#locale_format',
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
'*%H~%M~%S.%2f* would display *09~15~23.46*'
].join(' ')
},
// lines and grids
showline: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'Determines whether or not a line bounding this axis is drawn.'
].join(' ')
},
linecolor: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: 'Sets the axis line color.'
},
linewidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the axis line.'
},
showgrid: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not grid lines are drawn.',
'If *true*, the grid lines are drawn at every tick mark.'
].join(' ')
},
gridcolor: {
valType: 'color',
dflt: colorAttrs.lightLine,
role: 'style',
description: 'Sets the color of the grid lines.'
},
gridwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the grid lines.'
},
zeroline: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not a line is drawn at along the 0 value',
'of this axis.',
'If *true*, the zero line is drawn on top of the grid lines.'
].join(' ')
},
zerolinecolor: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: 'Sets the line color of the zero line.'
},
zerolinewidth: {
valType: 'number',
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the zero line.'
},
// positioning attributes
// anchor: not used directly, just put here for reference
// values are any opposite-letter axis id
anchor: {
valType: 'enumerated',
values: [
'free',
constants.idRegex.x.toString(),
constants.idRegex.y.toString()
],
role: 'info',
description: [
'If set to an opposite-letter axis id (e.g. `x2`, `y`), this axis is bound to',
'the corresponding opposite-letter axis.',
'If set to *free*, this axis\' position is determined by `position`.'
].join(' ')
},
// side: not used directly, as values depend on direction
// values are top, bottom for x axes, and left, right for y
side: {
valType: 'enumerated',
values: ['top', 'bottom', 'left', 'right'],
role: 'info',
description: [
'Determines whether a x (y) axis is positioned',
'at the *bottom* (*left*) or *top* (*right*)',
'of the plotting area.'
].join(' ')
},
// overlaying: not used directly, just put here for reference
// values are false and any other same-letter axis id that's not
// itself overlaying anything
overlaying: {
valType: 'enumerated',
values: [
'free',
constants.idRegex.x.toString(),
constants.idRegex.y.toString()
],
role: 'info',
description: [
'If set a same-letter axis id, this axis is overlaid on top of',
'the corresponding same-letter axis.',
'If *false*, this axis does not overlay any same-letter axes.'
].join(' ')
},
domain: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1},
{valType: 'number', min: 0, max: 1}
],
dflt: [0, 1],
description: [
'Sets the domain of this axis (in plot fraction).'
].join(' ')
},
position: {
valType: 'number',
min: 0,
max: 1,
dflt: 0,
role: 'style',
description: [
'Sets the position of this axis in the plotting space',
'(in normalized coordinates).',
'Only has an effect if `anchor` is set to *free*.'
].join(' ')
},
categoryorder: {
valType: 'enumerated',
values: [
'trace', 'category ascending', 'category descending', 'array'
/* , 'value ascending', 'value descending'*/ // value ascending / descending to be implemented later
],
dflt: 'trace',
role: 'info',
description: [
'Specifies the ordering logic for the case of categorical variables.',
'By default, plotly uses *trace*, which specifies the order that is present in the data supplied.',
'Set `categoryorder` to *category ascending* or *category descending* if order should be determined by',
'the alphanumerical order of the category names.',
/* 'Set `categoryorder` to *value ascending* or *value descending* if order should be determined by the',
'numerical order of the values.',*/ // // value ascending / descending to be implemented later
'Set `categoryorder` to *array* to derive the ordering from the attribute `categoryarray`. If a category',
'is not found in the `categoryarray` array, the sorting behavior for that attribute will be identical to',
'the *trace* mode. The unspecified categories will follow the categories in `categoryarray`.'
].join(' ')
},
categoryarray: {
valType: 'data_array',
role: 'info',
description: [
'Sets the order in which categories on this axis appear.',
'Only has an effect if `categoryorder` is set to *array*.',
'Used with `categoryorder`.'
].join(' ')
},
_deprecated: {
autotick: {
valType: 'boolean',
role: 'info',
description: [
'Obsolete.',
'Set `tickmode` to *auto* for old `autotick` *true* behavior.',
'Set `tickmode` to *linear* for `autotick` *false*.'
].join(' ')
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var Color = require('../../components/color');
var basePlotLayoutAttributes = require('../layout_attributes');
var constants = require('./constants');
var layoutAttributes = require('./layout_attributes');
var handleTypeDefaults = require('./type_defaults');
var handleAxisDefaults = require('./axis_defaults');
var handleConstraintDefaults = require('./constraint_defaults');
var handlePositionDefaults = require('./position_defaults');
var axisIds = require('./axis_ids');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
var layoutKeys = Object.keys(layoutIn),
xaListCartesian = [],
yaListCartesian = [],
xaListGl2d = [],
yaListGl2d = [],
xaListCheater = [],
xaListNonCheater = [],
outerTicks = {},
noGrids = {},
i;
// look for axes in the data
for(i = 0; i < fullData.length; i++) {
var trace = fullData[i];
var listX, listY;
if(Registry.traceIs(trace, 'cartesian')) {
listX = xaListCartesian;
listY = yaListCartesian;
}
else if(Registry.traceIs(trace, 'gl2d')) {
listX = xaListGl2d;
listY = yaListGl2d;
}
else continue;
var xaName = axisIds.id2name(trace.xaxis),
yaName = axisIds.id2name(trace.yaxis);
// Two things trigger axis visibility:
// 1. is not carpet
// 2. carpet that's not cheater
if(!Registry.traceIs(trace, 'carpet') || (trace.type === 'carpet' && !trace._cheater)) {
if(xaName) Lib.pushUnique(xaListNonCheater, xaName);
}
// The above check for definitely-not-cheater is not adequate. This
// second list tracks which axes *could* be a cheater so that the
// full condition triggering hiding is:
// *could* be a cheater and *is not definitely visible*
if(trace.type === 'carpet' && trace._cheater) {
if(xaName) Lib.pushUnique(xaListCheater, xaName);
}
// add axes implied by traces
if(xaName && listX.indexOf(xaName) === -1) listX.push(xaName);
if(yaName && listY.indexOf(yaName) === -1) listY.push(yaName);
// check for default formatting tweaks
if(Registry.traceIs(trace, '2dMap')) {
outerTicks[xaName] = true;
outerTicks[yaName] = true;
}
if(Registry.traceIs(trace, 'oriented')) {
var positionAxis = trace.orientation === 'h' ? yaName : xaName;
noGrids[positionAxis] = true;
}
}
// N.B. Ignore orphan axes (i.e. axes that have no data attached to them)
// if gl3d or geo is present on graph. This is retain backward compatible.
//
// TODO drop this in version 2.0
var ignoreOrphan = (layoutOut._has('gl3d') || layoutOut._has('geo'));
if(!ignoreOrphan) {
for(i = 0; i < layoutKeys.length; i++) {
var key = layoutKeys[i];
// orphan layout axes are considered cartesian subplots
if(xaListGl2d.indexOf(key) === -1 &&
xaListCartesian.indexOf(key) === -1 &&
constants.xAxisMatch.test(key)) {
xaListCartesian.push(key);
}
else if(yaListGl2d.indexOf(key) === -1 &&
yaListCartesian.indexOf(key) === -1 &&
constants.yAxisMatch.test(key)) {
yaListCartesian.push(key);
}
}
}
// make sure that plots with orphan cartesian axes
// are considered 'cartesian'
if(xaListCartesian.length && yaListCartesian.length) {
Lib.pushUnique(layoutOut._basePlotModules, Registry.subplotsRegistry.cartesian);
}
function axSort(a, b) {
var aNum = Number(a.substr(5) || 1),
bNum = Number(b.substr(5) || 1);
return aNum - bNum;
}
var xaList = xaListCartesian.concat(xaListGl2d).sort(axSort),
yaList = yaListCartesian.concat(yaListGl2d).sort(axSort),
axesList = xaList.concat(yaList);
// plot_bgcolor only makes sense if there's a (2D) plot!
// TODO: bgcolor for each subplot, to inherit from the main one
var plot_bgcolor = Color.background;
if(xaList.length && yaList.length) {
plot_bgcolor = Lib.coerce(layoutIn, layoutOut, basePlotLayoutAttributes, 'plot_bgcolor');
}
var bgColor = Color.combine(plot_bgcolor, layoutOut.paper_bgcolor);
var axName, axLetter, axLayoutIn, axLayoutOut;
function coerce(attr, dflt) {
return Lib.coerce(axLayoutIn, axLayoutOut, layoutAttributes, attr, dflt);
}
function getCounterAxes(axLetter) {
var list = {x: yaList, y: xaList}[axLetter];
return Lib.simpleMap(list, axisIds.name2id);
}
var counterAxes = {x: getCounterAxes('x'), y: getCounterAxes('y')};
function getOverlayableAxes(axLetter, axName) {
var list = {x: xaList, y: yaList}[axLetter];
var out = [];
for(var j = 0; j < list.length; j++) {
var axName2 = list[j];
if(axName2 !== axName && !(layoutIn[axName2] || {}).overlaying) {
out.push(axisIds.name2id(axName2));
}
}
return out;
}
// first pass creates the containers, determines types, and handles most of the settings
for(i = 0; i < axesList.length; i++) {
axName = axesList[i];
if(!Lib.isPlainObject(layoutIn[axName])) {
layoutIn[axName] = {};
}
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName] = {};
handleTypeDefaults(axLayoutIn, axLayoutOut, coerce, fullData, axName);
axLetter = axName.charAt(0);
var overlayableAxes = getOverlayableAxes(axLetter, axName);
var defaultOptions = {
letter: axLetter,
font: layoutOut.font,
outerTicks: outerTicks[axName],
showGrid: !noGrids[axName],
data: fullData,
bgColor: bgColor,
calendar: layoutOut.calendar,
cheateronly: axLetter === 'x' && (xaListCheater.indexOf(axName) !== -1 && xaListNonCheater.indexOf(axName) === -1)
};
handleAxisDefaults(axLayoutIn, axLayoutOut, coerce, defaultOptions, layoutOut);
var showSpikes = coerce('showspikes');
if(showSpikes) {
coerce('spikecolor');
coerce('spikethickness');
coerce('spikedash');
coerce('spikemode');
}
var positioningOptions = {
letter: axLetter,
counterAxes: counterAxes[axLetter],
overlayableAxes: overlayableAxes
};
handlePositionDefaults(axLayoutIn, axLayoutOut, coerce, positioningOptions);
axLayoutOut._input = axLayoutIn;
}
// quick second pass for range slider and selector defaults
var rangeSliderDefaults = Registry.getComponentMethod('rangeslider', 'handleDefaults'),
rangeSelectorDefaults = Registry.getComponentMethod('rangeselector', 'handleDefaults');
for(i = 0; i < xaList.length; i++) {
axName = xaList[i];
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
rangeSliderDefaults(layoutIn, layoutOut, axName);
if(axLayoutOut.type === 'date') {
rangeSelectorDefaults(
axLayoutIn,
axLayoutOut,
layoutOut,
yaList,
axLayoutOut.calendar
);
}
coerce('fixedrange');
}
for(i = 0; i < yaList.length; i++) {
axName = yaList[i];
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
var anchoredAxis = layoutOut[axisIds.id2name(axLayoutOut.anchor)];
var fixedRangeDflt = (
anchoredAxis &&
anchoredAxis.rangeslider &&
anchoredAxis.rangeslider.visible
);
coerce('fixedrange', fixedRangeDflt);
}
// Finally, handle scale constraints. We need to do this after all axes have
// coerced both `type` (so we link only axes of the same type) and
// `fixedrange` (so we can avoid linking from OR TO a fixed axis).
// sets of axes linked by `scaleanchor` along with the scaleratios compounded
// together, populated in handleConstraintDefaults
layoutOut._axisConstraintGroups = [];
var allAxisIds = counterAxes.x.concat(counterAxes.y);
for(i = 0; i < axesList.length; i++) {
axName = axesList[i];
axLetter = axName.charAt(0);
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
handleConstraintDefaults(axLayoutIn, axLayoutOut, coerce, allAxisIds, layoutOut);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
// flattenUniqueSort :: String -> Function -> [[String]] -> [String]
function flattenUniqueSort(axisLetter, sortFunction, data) {
// Bisection based insertion sort of distinct values for logarithmic time complexity.
// Can't use a hashmap, which is O(1), because ES5 maps coerce keys to strings. If it ever becomes a bottleneck,
// code can be separated: a hashmap (JS object) based version if all values encountered are strings; and
// downgrading to this O(log(n)) array on the first encounter of a non-string value.
var categoryArray = [];
var traceLines = data.map(function(d) {return d[axisLetter];});
var i, j, tracePoints, category, insertionIndex;
var bisector = d3.bisector(sortFunction).left;
for(i = 0; i < traceLines.length; i++) {
tracePoints = traceLines[i];
for(j = 0; j < tracePoints.length; j++) {
category = tracePoints[j];
// skip loop: ignore null and undefined categories
if(category === null || category === undefined) continue;
insertionIndex = bisector(categoryArray, category);
// skip loop on already encountered values
if(insertionIndex < categoryArray.length && categoryArray[insertionIndex] === category) continue;
// insert value
categoryArray.splice(insertionIndex, 0, category);
}
}
return categoryArray;
}
/**
* This pure function returns the ordered categories for specified axisLetter, categoryorder, categoryarray and data.
*
* If categoryorder is 'array', the result is a fresh copy of categoryarray, or if unspecified, an empty array.
*
* If categoryorder is 'category ascending' or 'category descending', the result is an array of ascending or descending
* order of the unique categories encountered in the data for specified axisLetter.
*
* See cartesian/layout_attributes.js for the definition of categoryorder and categoryarray
*
*/
// orderedCategories :: String -> String -> [String] -> [[String]] -> [String]
module.exports = function orderedCategories(axisLetter, categoryorder, categoryarray, data) {
switch(categoryorder) {
case 'array': return Array.isArray(categoryarray) ? categoryarray.slice() : [];
case 'category ascending': return flattenUniqueSort(axisLetter, d3.ascending, data);
case 'category descending': return flattenUniqueSort(axisLetter, d3.descending, data);
case 'trace': return [];
default: return [];
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
module.exports = function handlePositionDefaults(containerIn, containerOut, coerce, options) {
var counterAxes = options.counterAxes || [],
overlayableAxes = options.overlayableAxes || [],
letter = options.letter;
var anchor = Lib.coerce(containerIn, containerOut, {
anchor: {
valType: 'enumerated',
values: ['free'].concat(counterAxes),
dflt: isNumeric(containerIn.position) ? 'free' :
(counterAxes[0] || 'free')
}
}, 'anchor');
if(anchor === 'free') coerce('position');
Lib.coerce(containerIn, containerOut, {
side: {
valType: 'enumerated',
values: letter === 'x' ? ['bottom', 'top'] : ['left', 'right'],
dflt: letter === 'x' ? 'bottom' : 'left'
}
}, 'side');
var overlaying = false;
if(overlayableAxes.length) {
overlaying = Lib.coerce(containerIn, containerOut, {
overlaying: {
valType: 'enumerated',
values: [false].concat(overlayableAxes),
dflt: false
}
}, 'overlaying');
}
if(!overlaying) {
// TODO: right now I'm copying this domain over to overlaying axes
// in ax.setscale()... but this means we still need (imperfect) logic
// in the axes popover to hide domain for the overlaying axis.
// perhaps I should make a private version _domain that all axes get???
var domain = coerce('domain');
if(domain[0] > domain[1] - 0.01) containerOut.domain = [0, 1];
Lib.noneOrAll(containerIn.domain, containerOut.domain, [0, 1]);
}
return containerOut;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function scaleZoom(ax, factor, centerFraction) { if(centerFraction === undefined) centerFraction = 0.5; var rangeLinear = [ax.r2l(ax.range[0]), ax.r2l(ax.range[1])]; var center = rangeLinear[0] + (rangeLinear[1] - rangeLinear[0]) * centerFraction; var newHalfSpan = (center - rangeLinear[0]) * factor; ax.range = ax._input.range = [ ax.l2r(center - newHalfSpan), ax.l2r(center + newHalfSpan) ]; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var polygon = require('../../lib/polygon');
var color = require('../../components/color');
var axes = require('./axes');
var constants = require('./constants');
var filteredPolygon = polygon.filter;
var polygonTester = polygon.tester;
var MINSELECT = constants.MINSELECT;
function getAxId(ax) { return ax._id; }
module.exports = function prepSelect(e, startX, startY, dragOptions, mode) {
var plot = dragOptions.gd._fullLayout._zoomlayer,
dragBBox = dragOptions.element.getBoundingClientRect(),
xs = dragOptions.plotinfo.xaxis._offset,
ys = dragOptions.plotinfo.yaxis._offset,
x0 = startX - dragBBox.left,
y0 = startY - dragBBox.top,
x1 = x0,
y1 = y0,
path0 = 'M' + x0 + ',' + y0,
pw = dragOptions.xaxes[0]._length,
ph = dragOptions.yaxes[0]._length,
xAxisIds = dragOptions.xaxes.map(getAxId),
yAxisIds = dragOptions.yaxes.map(getAxId),
allAxes = dragOptions.xaxes.concat(dragOptions.yaxes),
pts;
if(mode === 'lasso') {
pts = filteredPolygon([[x0, y0]], constants.BENDPX);
}
var outlines = plot.selectAll('path.select-outline').data([1, 2]);
outlines.enter()
.append('path')
.attr('class', function(d) { return 'select-outline select-outline-' + d; })
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', path0 + 'Z');
var corners = plot.append('path')
.attr('class', 'zoombox-corners')
.style({
fill: color.background,
stroke: color.defaultLine,
'stroke-width': 1
})
.attr('transform', 'translate(' + xs + ', ' + ys + ')')
.attr('d', 'M0,0Z');
// find the traces to search for selection points
var searchTraces = [],
gd = dragOptions.gd,
i,
cd,
trace,
searchInfo,
selection = [],
eventData;
for(i = 0; i < gd.calcdata.length; i++) {
cd = gd.calcdata[i];
trace = cd[0].trace;
if(!trace._module || !trace._module.selectPoints) continue;
if(dragOptions.subplot) {
if(trace.subplot !== dragOptions.subplot) continue;
searchTraces.push({
selectPoints: trace._module.selectPoints,
cd: cd,
xaxis: dragOptions.xaxes[0],
yaxis: dragOptions.yaxes[0]
});
}
else {
if(xAxisIds.indexOf(trace.xaxis) === -1) continue;
if(yAxisIds.indexOf(trace.yaxis) === -1) continue;
searchTraces.push({
selectPoints: trace._module.selectPoints,
cd: cd,
xaxis: axes.getFromId(gd, trace.xaxis),
yaxis: axes.getFromId(gd, trace.yaxis)
});
}
}
function axValue(ax) {
var index = (ax._id.charAt(0) === 'y') ? 1 : 0;
return function(v) { return ax.p2d(v[index]); };
}
function ascending(a, b) { return a - b; }
dragOptions.moveFn = function(dx0, dy0) {
var poly,
ax;
x1 = Math.max(0, Math.min(pw, dx0 + x0));
y1 = Math.max(0, Math.min(ph, dy0 + y0));
var dx = Math.abs(x1 - x0),
dy = Math.abs(y1 - y0);
if(mode === 'select') {
if(dy < Math.min(dx * 0.6, MINSELECT)) {
// horizontal motion: make a vertical box
poly = polygonTester([[x0, 0], [x0, ph], [x1, ph], [x1, 0]]);
// extras to guide users in keeping a straight selection
corners.attr('d', 'M' + poly.xmin + ',' + (y0 - MINSELECT) +
'h-4v' + (2 * MINSELECT) + 'h4Z' +
'M' + (poly.xmax - 1) + ',' + (y0 - MINSELECT) +
'h4v' + (2 * MINSELECT) + 'h-4Z');
}
else if(dx < Math.min(dy * 0.6, MINSELECT)) {
// vertical motion: make a horizontal box
poly = polygonTester([[0, y0], [0, y1], [pw, y1], [pw, y0]]);
corners.attr('d', 'M' + (x0 - MINSELECT) + ',' + poly.ymin +
'v-4h' + (2 * MINSELECT) + 'v4Z' +
'M' + (x0 - MINSELECT) + ',' + (poly.ymax - 1) +
'v4h' + (2 * MINSELECT) + 'v-4Z');
}
else {
// diagonal motion
poly = polygonTester([[x0, y0], [x0, y1], [x1, y1], [x1, y0]]);
corners.attr('d', 'M0,0Z');
}
outlines.attr('d', 'M' + poly.xmin + ',' + poly.ymin +
'H' + (poly.xmax - 1) + 'V' + (poly.ymax - 1) +
'H' + poly.xmin + 'Z');
}
else if(mode === 'lasso') {
pts.addPt([x1, y1]);
poly = polygonTester(pts.filtered);
outlines.attr('d', 'M' + pts.filtered.join('L') + 'Z');
}
selection = [];
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
[].push.apply(selection, searchInfo.selectPoints(searchInfo, poly));
}
eventData = {points: selection};
if(mode === 'select') {
var ranges = eventData.range = {},
axLetter;
for(i = 0; i < allAxes.length; i++) {
ax = allAxes[i];
axLetter = ax._id.charAt(0);
ranges[ax._id] = [
ax.p2d(poly[axLetter + 'min']),
ax.p2d(poly[axLetter + 'max'])].sort(ascending);
}
}
else {
var dataPts = eventData.lassoPoints = {};
for(i = 0; i < allAxes.length; i++) {
ax = allAxes[i];
dataPts[ax._id] = pts.filtered.map(axValue(ax));
}
}
dragOptions.gd.emit('plotly_selecting', eventData);
};
dragOptions.doneFn = function(dragged, numclicks) {
corners.remove();
if(!dragged && numclicks === 2) {
// clear selection on doubleclick
outlines.remove();
for(i = 0; i < searchTraces.length; i++) {
searchInfo = searchTraces[i];
searchInfo.selectPoints(searchInfo, false);
}
gd.emit('plotly_deselect', null);
}
else {
dragOptions.gd.emit('plotly_selected', eventData);
}
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var cleanNumber = Lib.cleanNumber;
var ms2DateTime = Lib.ms2DateTime;
var dateTime2ms = Lib.dateTime2ms;
var numConstants = require('../../constants/numerical');
var FP_SAFE = numConstants.FP_SAFE;
var BADNUM = numConstants.BADNUM;
var constants = require('./constants');
var axisIds = require('./axis_ids');
function fromLog(v) {
return Math.pow(10, v);
}
function num(v) {
if(!isNumeric(v)) return BADNUM;
v = Number(v);
if(v < -FP_SAFE || v > FP_SAFE) return BADNUM;
return isNumeric(v) ? Number(v) : BADNUM;
}
/**
* Define the conversion functions for an axis data is used in 5 ways:
*
* d: data, in whatever form it's provided
* c: calcdata: turned into numbers, but not linearized
* l: linearized - same as c except for log axes (and other nonlinear
* mappings later?) this is used when we need to know if it's
* *possible* to show some data on this axis, without caring about
* the current range
* p: pixel value - mapped to the screen with current size and zoom
* r: ranges, tick0, and annotation positions match one of the above
* but are handled differently for different types:
* - linear and date: data format (d)
* - category: calcdata format (c), and will stay that way because
* the data format has no continuous mapping
* - log: linearized (l) format
* TODO: in v2.0 we plan to change it to data format. At that point
* shapes will work the same way as ranges, tick0, and annotations
* so they can use this conversion too.
*
* Creates/updates these conversion functions, and a few more utilities
* like cleanRange, and makeCalcdata
*
* also clears the autorange bounds ._min and ._max
* and the autotick constraints ._minDtick, ._forceTick0
*/
module.exports = function setConvert(ax, fullLayout) {
fullLayout = fullLayout || {};
// clipMult: how many axis lengths past the edge do we render?
// for panning, 1-2 would suffice, but for zooming more is nice.
// also, clipping can affect the direction of lines off the edge...
var clipMult = 10;
function toLog(v, clip) {
if(v > 0) return Math.log(v) / Math.LN10;
else if(v <= 0 && clip && ax.range && ax.range.length === 2) {
// clip NaN (ie past negative infinity) to clipMult axis
// length past the negative edge
var r0 = ax.range[0],
r1 = ax.range[1];
return 0.5 * (r0 + r1 - 3 * clipMult * Math.abs(r0 - r1));
}
else return BADNUM;
}
/*
* wrapped dateTime2ms that:
* - accepts ms numbers for backward compatibility
* - inserts a dummy arg so calendar is the 3rd arg (see notes below).
* - defaults to ax.calendar
*/
function dt2ms(v, _, calendar) {
// NOTE: Changed this behavior: previously we took any numeric value
// to be a ms, even if it was a string that could be a bare year.
// Now we convert it as a date if at all possible, and only try
// as (local) ms if that fails.
var ms = dateTime2ms(v, calendar || ax.calendar);
if(ms === BADNUM) {
if(isNumeric(v)) ms = dateTime2ms(new Date(+v));
else return BADNUM;
}
return ms;
}
// wrapped ms2DateTime to insert default ax.calendar
function ms2dt(v, r, calendar) {
return ms2DateTime(v, r, calendar || ax.calendar);
}
function getCategoryName(v) {
return ax._categories[Math.round(v)];
}
/*
* setCategoryIndex: return the index of category v,
* inserting it in the list if it's not already there
*
* this will enter the categories in the order it
* encounters them, ie all the categories from the
* first data set, then all the ones from the second
* that aren't in the first etc.
*
* it is assumed that this function is being invoked in the
* already sorted category order; otherwise there would be
* a disconnect between the array and the index returned
*/
function setCategoryIndex(v) {
if(v !== null && v !== undefined) {
if(ax._categoriesMap === undefined) {
ax._categoriesMap = {};
}
if(ax._categoriesMap[v] !== undefined) {
return ax._categoriesMap[v];
} else {
ax._categories.push(v);
var curLength = ax._categories.length - 1;
ax._categoriesMap[v] = curLength;
return curLength;
}
}
return BADNUM;
}
function getCategoryIndex(v) {
// d2l/d2c variant that that won't add categories but will also
// allow numbers to be mapped to the linearized axis positions
if(ax._categoriesMap) {
var index = ax._categoriesMap[v];
if(index !== undefined) return index;
}
if(typeof v === 'number') { return v; }
}
function l2p(v) {
if(!isNumeric(v)) return BADNUM;
// include 2 fractional digits on pixel, for PDF zooming etc
return d3.round(ax._b + ax._m * v, 2);
}
function p2l(px) { return (px - ax._b) / ax._m; }
// conversions among c/l/p are fairly simple - do them together for all axis types
ax.c2l = (ax.type === 'log') ? toLog : num;
ax.l2c = (ax.type === 'log') ? fromLog : num;
ax.l2p = l2p;
ax.p2l = p2l;
ax.c2p = (ax.type === 'log') ? function(v, clip) { return l2p(toLog(v, clip)); } : l2p;
ax.p2c = (ax.type === 'log') ? function(px) { return fromLog(p2l(px)); } : p2l;
/*
* now type-specific conversions for **ALL** other combinations
* they're all written out, instead of being combinations of each other, for
* both clarity and speed.
*/
if(['linear', '-'].indexOf(ax.type) !== -1) {
// all are data vals, but d and r need cleaning
ax.d2r = ax.r2d = ax.d2c = ax.r2c = ax.d2l = ax.r2l = cleanNumber;
ax.c2d = ax.c2r = ax.l2d = ax.l2r = num;
ax.d2p = ax.r2p = function(v) { return l2p(cleanNumber(v)); };
ax.p2d = ax.p2r = p2l;
}
else if(ax.type === 'log') {
// d and c are data vals, r and l are logged (but d and r need cleaning)
ax.d2r = ax.d2l = function(v, clip) { return toLog(cleanNumber(v), clip); };
ax.r2d = ax.r2c = function(v) { return fromLog(cleanNumber(v)); };
ax.d2c = ax.r2l = cleanNumber;
ax.c2d = ax.l2r = num;
ax.c2r = toLog;
ax.l2d = fromLog;
ax.d2p = function(v, clip) { return l2p(ax.d2r(v, clip)); };
ax.p2d = function(px) { return fromLog(p2l(px)); };
ax.r2p = function(v) { return l2p(cleanNumber(v)); };
ax.p2r = p2l;
}
else if(ax.type === 'date') {
// r and d are date strings, l and c are ms
/*
* Any of these functions with r and d on either side, calendar is the
* **3rd** argument. log has reserved the second argument.
*
* Unless you need the special behavior of the second arg (ms2DateTime
* uses this to limit precision, toLog uses true to clip negatives
* to offscreen low rather than undefined), it's safe to pass 0.
*/
ax.d2r = ax.r2d = Lib.identity;
ax.d2c = ax.r2c = ax.d2l = ax.r2l = dt2ms;
ax.c2d = ax.c2r = ax.l2d = ax.l2r = ms2dt;
ax.d2p = ax.r2p = function(v, _, calendar) { return l2p(dt2ms(v, 0, calendar)); };
ax.p2d = ax.p2r = function(px, r, calendar) { return ms2dt(p2l(px), r, calendar); };
}
else if(ax.type === 'category') {
// d is categories; r, c, and l are indices
// TODO: should r accept category names too?
// ie r2c and r2l would be getCategoryIndex (and r2p would change)
ax.d2r = ax.d2c = ax.d2l = setCategoryIndex;
ax.r2d = ax.c2d = ax.l2d = getCategoryName;
// special d2l variant that won't add categories
ax.d2l_noadd = getCategoryIndex;
ax.r2l = ax.l2r = ax.r2c = ax.c2r = num;
ax.d2p = function(v) { return l2p(getCategoryIndex(v)); };
ax.p2d = function(px) { return getCategoryName(p2l(px)); };
ax.r2p = l2p;
ax.p2r = p2l;
}
// find the range value at the specified (linear) fraction of the axis
ax.fraction2r = function(v) {
var rl0 = ax.r2l(ax.range[0]),
rl1 = ax.r2l(ax.range[1]);
return ax.l2r(rl0 + v * (rl1 - rl0));
};
// find the fraction of the range at the specified range value
ax.r2fraction = function(v) {
var rl0 = ax.r2l(ax.range[0]),
rl1 = ax.r2l(ax.range[1]);
return (ax.r2l(v) - rl0) / (rl1 - rl0);
};
/*
* cleanRange: make sure range is a couplet of valid & distinct values
* keep numbers away from the limits of floating point numbers,
* and dates away from the ends of our date system (+/- 9999 years)
*
* optional param rangeAttr: operate on a different attribute, like
* ax._r, rather than ax.range
*/
ax.cleanRange = function(rangeAttr) {
if(!rangeAttr) rangeAttr = 'range';
var range = Lib.nestedProperty(ax, rangeAttr).get(),
axLetter = (ax._id || 'x').charAt(0),
i, dflt;
if(ax.type === 'date') dflt = Lib.dfltRange(ax.calendar);
else if(axLetter === 'y') dflt = constants.DFLTRANGEY;
else dflt = constants.DFLTRANGEX;
// make sure we don't later mutate the defaults
dflt = dflt.slice();
if(!range || range.length !== 2) {
Lib.nestedProperty(ax, rangeAttr).set(dflt);
return;
}
if(ax.type === 'date') {
// check if milliseconds or js date objects are provided for range
// and convert to date strings
range[0] = Lib.cleanDate(range[0], BADNUM, ax.calendar);
range[1] = Lib.cleanDate(range[1], BADNUM, ax.calendar);
}
for(i = 0; i < 2; i++) {
if(ax.type === 'date') {
if(!Lib.isDateTime(range[i], ax.calendar)) {
ax[rangeAttr] = dflt;
break;
}
if(ax.r2l(range[0]) === ax.r2l(range[1])) {
// split by +/- 1 second
var linCenter = Lib.constrain(ax.r2l(range[0]),
Lib.MIN_MS + 1000, Lib.MAX_MS - 1000);
range[0] = ax.l2r(linCenter - 1000);
range[1] = ax.l2r(linCenter + 1000);
break;
}
}
else {
if(!isNumeric(range[i])) {
if(isNumeric(range[1 - i])) {
range[i] = range[1 - i] * (i ? 10 : 0.1);
}
else {
ax[rangeAttr] = dflt;
break;
}
}
if(range[i] < -FP_SAFE) range[i] = -FP_SAFE;
else if(range[i] > FP_SAFE) range[i] = FP_SAFE;
if(range[0] === range[1]) {
// somewhat arbitrary: split by 1 or 1ppm, whichever is bigger
var inc = Math.max(1, Math.abs(range[0] * 1e-6));
range[0] -= inc;
range[1] += inc;
}
}
}
};
// set scaling to pixels
ax.setScale = function(usePrivateRange) {
var gs = fullLayout._size,
axLetter = ax._id.charAt(0);
// TODO cleaner way to handle this case
if(!ax._categories) ax._categories = [];
// Add a map to optimize the performance of category collection
if(!ax._categoriesMap) ax._categoriesMap = {};
// make sure we have a domain (pull it in from the axis
// this one is overlaying if necessary)
if(ax.overlaying) {
var ax2 = axisIds.getFromId({ _fullLayout: fullLayout }, ax.overlaying);
ax.domain = ax2.domain;
}
// While transitions are occuring, occurring, we get a double-transform
// issue if we transform the drawn layer *and* use the new axis range to
// draw the data. This allows us to construct setConvert using the pre-
// interaction values of the range:
var rangeAttr = (usePrivateRange && ax._r) ? '_r' : 'range',
calendar = ax.calendar;
ax.cleanRange(rangeAttr);
var rl0 = ax.r2l(ax[rangeAttr][0], calendar),
rl1 = ax.r2l(ax[rangeAttr][1], calendar);
if(axLetter === 'y') {
ax._offset = gs.t + (1 - ax.domain[1]) * gs.h;
ax._length = gs.h * (ax.domain[1] - ax.domain[0]);
ax._m = ax._length / (rl0 - rl1);
ax._b = -ax._m * rl1;
}
else {
ax._offset = gs.l + ax.domain[0] * gs.w;
ax._length = gs.w * (ax.domain[1] - ax.domain[0]);
ax._m = ax._length / (rl1 - rl0);
ax._b = -ax._m * rl0;
}
if(!isFinite(ax._m) || !isFinite(ax._b)) {
Lib.notifier(
'Something went wrong with axis scaling',
'long');
fullLayout._replotting = false;
throw new Error('axis scaling');
}
};
// makeCalcdata: takes an x or y array and converts it
// to a position on the axis object "ax"
// inputs:
// trace - a data object from gd.data
// axLetter - a string, either 'x' or 'y', for which item
// to convert (TODO: is this now always the same as
// the first letter of ax._id?)
// in case the expected data isn't there, make a list of
// integers based on the opposite data
ax.makeCalcdata = function(trace, axLetter) {
var arrayIn, arrayOut, i;
var cal = ax.type === 'date' && trace[axLetter + 'calendar'];
if(axLetter in trace) {
arrayIn = trace[axLetter];
arrayOut = new Array(arrayIn.length);
for(i = 0; i < arrayIn.length; i++) {
arrayOut[i] = ax.d2c(arrayIn[i], 0, cal);
}
}
else {
var v0 = ((axLetter + '0') in trace) ?
ax.d2c(trace[axLetter + '0'], 0, cal) : 0,
dv = (trace['d' + axLetter]) ?
Number(trace['d' + axLetter]) : 1;
// the opposing data, for size if we have x and dx etc
arrayIn = trace[{x: 'y', y: 'x'}[axLetter]];
arrayOut = new Array(arrayIn.length);
for(i = 0; i < arrayIn.length; i++) arrayOut[i] = v0 + i * dv;
}
return arrayOut;
};
ax.isValidRange = function(range) {
return (
Array.isArray(range) &&
range.length === 2 &&
isNumeric(ax.r2l(range[0])) &&
isNumeric(ax.r2l(range[1]))
);
};
// for autoranging: arrays of objects:
// {val: axis value, pad: pixel padding}
// on the low and high sides
ax._min = [];
ax._max = [];
// copy ref to fullLayout.separators so that
// methods in Axes can use it w/o having to pass fullLayout
ax._separators = fullLayout.separators;
// and for bar charts and box plots: reset forced minimum tick spacing
delete ax._minDtick;
delete ax._forceTick0;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
/**
* options: inherits font, outerTicks, noHover from axes.handleAxisDefaults
*/
module.exports = function handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options) {
var showAttrDflt = getShowAttrDflt(containerIn);
var tickPrefix = coerce('tickprefix');
if(tickPrefix) coerce('showtickprefix', showAttrDflt);
var tickSuffix = coerce('ticksuffix');
if(tickSuffix) coerce('showticksuffix', showAttrDflt);
var showTickLabels = coerce('showticklabels');
if(showTickLabels) {
var font = options.font || {};
// as with titlefont.color, inherit axis.color only if one was
// explicitly provided
var dfltFontColor = (containerOut.color === containerIn.color) ?
containerOut.color : font.color;
Lib.coerceFont(coerce, 'tickfont', {
family: font.family,
size: font.size,
color: dfltFontColor
});
coerce('tickangle');
if(axType !== 'category') {
var tickFormat = coerce('tickformat');
if(!tickFormat && axType !== 'date') {
coerce('showexponent', showAttrDflt);
coerce('exponentformat');
coerce('separatethousands');
}
}
}
if(axType !== 'category' && !options.noHover) coerce('hoverformat');
};
/*
* Attributes 'showexponent', 'showtickprefix' and 'showticksuffix'
* share values.
*
* If only 1 attribute is set,
* the remaining attributes inherit that value.
*
* If 2 attributes are set to the same value,
* the remaining attribute inherits that value.
*
* If 2 attributes are set to different values,
* the remaining is set to its dflt value.
*
*/
function getShowAttrDflt(containerIn) {
var showAttrsAll = ['showexponent',
'showtickprefix',
'showticksuffix'],
showAttrs = showAttrsAll.filter(function(a) {
return containerIn[a] !== undefined;
}),
sameVal = function(a) {
return containerIn[a] === containerIn[showAttrs[0]];
};
if(showAttrs.every(sameVal) || showAttrs.length === 1) {
return containerIn[showAttrs[0]];
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var layoutAttributes = require('./layout_attributes');
/**
* options: inherits outerTicks from axes.handleAxisDefaults
*/
module.exports = function handleTickDefaults(containerIn, containerOut, coerce, options) {
var tickLen = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'ticklen'),
tickWidth = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'tickwidth'),
tickColor = Lib.coerce2(containerIn, containerOut, layoutAttributes, 'tickcolor', containerOut.color),
showTicks = coerce('ticks', (options.outerTicks || tickLen || tickWidth || tickColor) ? 'outside' : '');
if(!showTicks) {
delete containerOut.ticklen;
delete containerOut.tickwidth;
delete containerOut.tickcolor;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var ONEDAY = require('../../constants/numerical').ONEDAY;
module.exports = function handleTickValueDefaults(containerIn, containerOut, coerce, axType) {
var tickmodeDefault = 'auto';
if(containerIn.tickmode === 'array' &&
(axType === 'log' || axType === 'date')) {
containerIn.tickmode = 'auto';
}
if(Array.isArray(containerIn.tickvals)) tickmodeDefault = 'array';
else if(containerIn.dtick) {
tickmodeDefault = 'linear';
}
var tickmode = coerce('tickmode', tickmodeDefault);
if(tickmode === 'auto') coerce('nticks');
else if(tickmode === 'linear') {
// dtick is usually a positive number, but there are some
// special strings available for log or date axes
// default is 1 day for dates, otherwise 1
var dtickDflt = (axType === 'date') ? ONEDAY : 1;
var dtick = coerce('dtick', dtickDflt);
if(isNumeric(dtick)) {
containerOut.dtick = (dtick > 0) ? Number(dtick) : dtickDflt;
}
else if(typeof dtick !== 'string') {
containerOut.dtick = dtickDflt;
}
else {
// date and log special cases are all one character plus a number
var prefix = dtick.charAt(0),
dtickNum = dtick.substr(1);
dtickNum = isNumeric(dtickNum) ? Number(dtickNum) : 0;
if((dtickNum <= 0) || !(
// "M<n>" gives ticks every (integer) n months
(axType === 'date' && prefix === 'M' && dtickNum === Math.round(dtickNum)) ||
// "L<f>" gives ticks linearly spaced in data (not in position) every (float) f
(axType === 'log' && prefix === 'L') ||
// "D1" gives powers of 10 with all small digits between, "D2" gives only 2 and 5
(axType === 'log' && prefix === 'D' && (dtickNum === 1 || dtickNum === 2))
)) {
containerOut.dtick = dtickDflt;
}
}
// tick0 can have different valType for different axis types, so
// validate that now. Also for dates, change milliseconds to date strings
var tick0Dflt = (axType === 'date') ? Lib.dateTick0(containerOut.calendar) : 0;
var tick0 = coerce('tick0', tick0Dflt);
if(axType === 'date') {
containerOut.tick0 = Lib.cleanDate(tick0, tick0Dflt);
}
// Aside from date axes, dtick must be numeric; D1 and D2 modes ignore tick0 entirely
else if(isNumeric(tick0) && dtick !== 'D1' && dtick !== 'D2') {
containerOut.tick0 = Number(tick0);
}
else {
containerOut.tick0 = tick0Dflt;
}
}
else {
var tickvals = coerce('tickvals');
if(tickvals === undefined) containerOut.tickmode = 'auto';
else coerce('ticktext');
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Plotly = require('../../plotly');
var Registry = require('../../registry');
var Drawing = require('../../components/drawing');
var Axes = require('./axes');
var axisRegex = /((x|y)([2-9]|[1-9][0-9]+)?)axis$/;
module.exports = function transitionAxes(gd, newLayout, transitionOpts, makeOnCompleteCallback) {
var fullLayout = gd._fullLayout;
var axes = [];
function computeUpdates(layout) {
var ai, attrList, match, axis, update;
var updates = {};
for(ai in layout) {
attrList = ai.split('.');
match = attrList[0].match(axisRegex);
if(match) {
var axisLetter = match[1];
var axisName = axisLetter + 'axis';
axis = fullLayout[axisName];
update = {};
if(Array.isArray(layout[ai])) {
update.to = layout[ai].slice(0);
} else {
if(Array.isArray(layout[ai].range)) {
update.to = layout[ai].range.slice(0);
}
}
if(!update.to) continue;
update.axisName = axisName;
update.length = axis._length;
axes.push(axisLetter);
updates[axisLetter] = update;
}
}
return updates;
}
function computeAffectedSubplots(fullLayout, updatedAxisIds, updates) {
var plotName;
var plotinfos = fullLayout._plots;
var affectedSubplots = [];
var toX, toY;
for(plotName in plotinfos) {
var plotinfo = plotinfos[plotName];
if(affectedSubplots.indexOf(plotinfo) !== -1) continue;
var x = plotinfo.xaxis._id;
var y = plotinfo.yaxis._id;
var fromX = plotinfo.xaxis.range;
var fromY = plotinfo.yaxis.range;
// Store the initial range at the beginning of this transition:
plotinfo.xaxis._r = plotinfo.xaxis.range.slice();
plotinfo.yaxis._r = plotinfo.yaxis.range.slice();
if(updates[x]) {
toX = updates[x].to;
} else {
toX = fromX;
}
if(updates[y]) {
toY = updates[y].to;
} else {
toY = fromY;
}
if(fromX[0] === toX[0] && fromX[1] === toX[1] && fromY[0] === toY[0] && fromY[1] === toY[1]) continue;
if(updatedAxisIds.indexOf(x) !== -1 || updatedAxisIds.indexOf(y) !== -1) {
affectedSubplots.push(plotinfo);
}
}
return affectedSubplots;
}
var updates = computeUpdates(newLayout);
var updatedAxisIds = Object.keys(updates);
var affectedSubplots = computeAffectedSubplots(fullLayout, updatedAxisIds, updates);
if(!affectedSubplots.length) {
return false;
}
function ticksAndAnnotations(xa, ya) {
var activeAxIds = [],
i;
activeAxIds = [xa._id, ya._id];
for(i = 0; i < activeAxIds.length; i++) {
Axes.doTicks(gd, activeAxIds[i], true);
}
function redrawObjs(objArray, method) {
for(i = 0; i < objArray.length; i++) {
var obji = objArray[i];
if((activeAxIds.indexOf(obji.xref) !== -1) ||
(activeAxIds.indexOf(obji.yref) !== -1)) {
method(gd, i);
}
}
}
// annotations and shapes 'draw' method is slow,
// use the finer-grained 'drawOne' method instead
redrawObjs(fullLayout.annotations || [], Registry.getComponentMethod('annotations', 'drawOne'));
redrawObjs(fullLayout.shapes || [], Registry.getComponentMethod('shapes', 'drawOne'));
redrawObjs(fullLayout.images || [], Registry.getComponentMethod('images', 'draw'));
}
function unsetSubplotTransform(subplot) {
var xa2 = subplot.xaxis;
var ya2 = subplot.yaxis;
fullLayout._defs.selectAll('#' + subplot.clipId)
.call(Drawing.setTranslate, 0, 0)
.call(Drawing.setScale, 1, 1);
subplot.plot
.call(Drawing.setTranslate, xa2._offset, ya2._offset)
.call(Drawing.setScale, 1, 1)
// This is specifically directed at scatter traces, applying an inverse
// scale to individual points to counteract the scale of the trace
// as a whole:
.selectAll('.points').selectAll('.point')
.call(Drawing.setPointGroupScale, 1, 1);
}
function updateSubplot(subplot, progress) {
var axis, r0, r1;
var xUpdate = updates[subplot.xaxis._id];
var yUpdate = updates[subplot.yaxis._id];
var viewBox = [];
if(xUpdate) {
axis = gd._fullLayout[xUpdate.axisName];
r0 = axis._r;
r1 = xUpdate.to;
viewBox[0] = (r0[0] * (1 - progress) + progress * r1[0] - r0[0]) / (r0[1] - r0[0]) * subplot.xaxis._length;
var dx1 = r0[1] - r0[0];
var dx2 = r1[1] - r1[0];
axis.range[0] = r0[0] * (1 - progress) + progress * r1[0];
axis.range[1] = r0[1] * (1 - progress) + progress * r1[1];
viewBox[2] = subplot.xaxis._length * ((1 - progress) + progress * dx2 / dx1);
} else {
viewBox[0] = 0;
viewBox[2] = subplot.xaxis._length;
}
if(yUpdate) {
axis = gd._fullLayout[yUpdate.axisName];
r0 = axis._r;
r1 = yUpdate.to;
viewBox[1] = (r0[1] * (1 - progress) + progress * r1[1] - r0[1]) / (r0[0] - r0[1]) * subplot.yaxis._length;
var dy1 = r0[1] - r0[0];
var dy2 = r1[1] - r1[0];
axis.range[0] = r0[0] * (1 - progress) + progress * r1[0];
axis.range[1] = r0[1] * (1 - progress) + progress * r1[1];
viewBox[3] = subplot.yaxis._length * ((1 - progress) + progress * dy2 / dy1);
} else {
viewBox[1] = 0;
viewBox[3] = subplot.yaxis._length;
}
ticksAndAnnotations(subplot.xaxis, subplot.yaxis);
var xa2 = subplot.xaxis;
var ya2 = subplot.yaxis;
var editX = !!xUpdate;
var editY = !!yUpdate;
var xScaleFactor = editX ? xa2._length / viewBox[2] : 1,
yScaleFactor = editY ? ya2._length / viewBox[3] : 1;
var clipDx = editX ? viewBox[0] : 0,
clipDy = editY ? viewBox[1] : 0;
var fracDx = editX ? (viewBox[0] / viewBox[2] * xa2._length) : 0,
fracDy = editY ? (viewBox[1] / viewBox[3] * ya2._length) : 0;
var plotDx = xa2._offset - fracDx,
plotDy = ya2._offset - fracDy;
fullLayout._defs.selectAll('#' + subplot.clipId)
.call(Drawing.setTranslate, clipDx, clipDy)
.call(Drawing.setScale, 1 / xScaleFactor, 1 / yScaleFactor);
subplot.plot
.call(Drawing.setTranslate, plotDx, plotDy)
.call(Drawing.setScale, xScaleFactor, yScaleFactor)
// This is specifically directed at scatter traces, applying an inverse
// scale to individual points to counteract the scale of the trace
// as a whole:
.selectAll('.points').selectAll('.point')
.call(Drawing.setPointGroupScale, 1 / xScaleFactor, 1 / yScaleFactor);
}
var onComplete;
if(makeOnCompleteCallback) {
// This module makes the choice whether or not it notifies Plotly.transition
// about completion:
onComplete = makeOnCompleteCallback();
}
function transitionComplete() {
var aobj = {};
for(var i = 0; i < updatedAxisIds.length; i++) {
var axi = gd._fullLayout[updates[updatedAxisIds[i]].axisName];
var to = updates[updatedAxisIds[i]].to;
aobj[axi._name + '.range[0]'] = to[0];
aobj[axi._name + '.range[1]'] = to[1];
axi.range = to.slice();
}
// Signal that this transition has completed:
onComplete && onComplete();
return Plotly.relayout(gd, aobj).then(function() {
for(var i = 0; i < affectedSubplots.length; i++) {
unsetSubplotTransform(affectedSubplots[i]);
}
});
}
function transitionInterrupt() {
var aobj = {};
for(var i = 0; i < updatedAxisIds.length; i++) {
var axi = gd._fullLayout[updatedAxisIds[i] + 'axis'];
aobj[axi._name + '.range[0]'] = axi.range[0];
aobj[axi._name + '.range[1]'] = axi.range[1];
axi.range = axi._r.slice();
}
return Plotly.relayout(gd, aobj).then(function() {
for(var i = 0; i < affectedSubplots.length; i++) {
unsetSubplotTransform(affectedSubplots[i]);
}
});
}
var t1, t2, raf;
var easeFn = d3.ease(transitionOpts.easing);
gd._transitionData._interruptCallbacks.push(function() {
window.cancelAnimationFrame(raf);
raf = null;
return transitionInterrupt();
});
function doFrame() {
t2 = Date.now();
var tInterp = Math.min(1, (t2 - t1) / transitionOpts.duration);
var progress = easeFn(tInterp);
for(var i = 0; i < affectedSubplots.length; i++) {
updateSubplot(affectedSubplots[i], progress);
}
if(t2 - t1 > transitionOpts.duration) {
transitionComplete();
raf = window.cancelAnimationFrame(doFrame);
} else {
raf = window.requestAnimationFrame(doFrame);
}
}
t1 = Date.now();
raf = window.requestAnimationFrame(doFrame);
return Promise.resolve();
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var autoType = require('./axis_autotype');
var name2id = require('./axis_ids').name2id;
/*
* data: the plot data to use in choosing auto type
* name: axis object name (ie 'xaxis') if one should be stored
*/
module.exports = function handleTypeDefaults(containerIn, containerOut, coerce, data, name) {
// set up some private properties
if(name) {
containerOut._name = name;
containerOut._id = name2id(name);
}
var axType = coerce('type');
if(axType === '-') {
setAutoType(containerOut, data);
if(containerOut.type === '-') {
containerOut.type = 'linear';
}
else {
// copy autoType back to input axis
// note that if this object didn't exist
// in the input layout, we have to put it in
// this happens in the main supplyDefaults function
containerIn.type = containerOut.type;
}
}
};
function setAutoType(ax, data) {
// new logic: let people specify any type they want,
// only autotype if type is '-'
if(ax.type !== '-') return;
var id = ax._id,
axLetter = id.charAt(0);
// support 3d
if(id.indexOf('scene') !== -1) id = axLetter;
var d0 = getFirstNonEmptyTrace(data, id, axLetter);
if(!d0) return;
// first check for histograms, as the count direction
// should always default to a linear axis
if(d0.type === 'histogram' &&
axLetter === {v: 'y', h: 'x'}[d0.orientation || 'v']) {
ax.type = 'linear';
return;
}
var calAttr = axLetter + 'calendar',
calendar = d0[calAttr];
// check all boxes on this x axis to see
// if they're dates, numbers, or categories
if(isBoxWithoutPositionCoords(d0, axLetter)) {
var posLetter = getBoxPosLetter(d0),
boxPositions = [],
trace;
for(var i = 0; i < data.length; i++) {
trace = data[i];
if(!Registry.traceIs(trace, 'box') ||
(trace[axLetter + 'axis'] || axLetter) !== id) continue;
if(trace[posLetter] !== undefined) boxPositions.push(trace[posLetter][0]);
else if(trace.name !== undefined) boxPositions.push(trace.name);
else boxPositions.push('text');
if(trace[calAttr] !== calendar) calendar = undefined;
}
ax.type = autoType(boxPositions, calendar);
}
else {
ax.type = autoType(d0[axLetter] || [d0[axLetter + '0']], calendar);
}
}
function getFirstNonEmptyTrace(data, id, axLetter) {
for(var i = 0; i < data.length; i++) {
var trace = data[i];
if((trace[axLetter + 'axis'] || axLetter) === id) {
if(isBoxWithoutPositionCoords(trace, axLetter)) {
return trace;
}
else if((trace[axLetter] || []).length || trace[axLetter + '0']) {
return trace;
}
}
}
}
function getBoxPosLetter(trace) {
return {v: 'x', h: 'y'}[trace.orientation || 'v'];
}
function isBoxWithoutPositionCoords(trace, axLetter) {
var posLetter = getBoxPosLetter(trace),
isBox = Registry.traceIs(trace, 'box'),
isCandlestick = Registry.traceIs(trace._fullInput || {}, 'candlestick');
return (
isBox &&
!isCandlestick &&
axLetter === posLetter &&
trace[posLetter] === undefined &&
trace[posLetter + '0'] === undefined
);
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| constants.js | 100% | (17 / 17) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (17 / 17) | |
| geo.js | 17.87% | (37 / 207) | 0% | (0 / 51) | 0% | (0 / 30) | 18.78% | (37 / 197) | |
| projections.js | 23.97% | (70 / 292) | 0% | (0 / 120) | 5% | (4 / 80) | 26.02% | (70 / 269) | |
| set_scale.js | 17.95% | (7 / 39) | 0% | (0 / 4) | 0% | (0 / 5) | 18.42% | (7 / 38) | |
| zoom.js | 19.63% | (32 / 163) | 0% | (0 / 45) | 0% | (0 / 35) | 20.78% | (32 / 154) | |
| zoom_reset.js | 11.11% | (1 / 9) | 100% | (0 / 0) | 0% | (0 / 2) | 11.11% | (1 / 9) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var params = module.exports = {};
// projection names to d3 function name
params.projNames = {
// d3.geo.projection
'equirectangular': 'equirectangular',
'mercator': 'mercator',
'orthographic': 'orthographic',
'natural earth': 'naturalEarth',
'kavrayskiy7': 'kavrayskiy7',
'miller': 'miller',
'robinson': 'robinson',
'eckert4': 'eckert4',
'azimuthal equal area': 'azimuthalEqualArea',
'azimuthal equidistant': 'azimuthalEquidistant',
'conic equal area': 'conicEqualArea',
'conic conformal': 'conicConformal',
'conic equidistant': 'conicEquidistant',
'gnomonic': 'gnomonic',
'stereographic': 'stereographic',
'mollweide': 'mollweide',
'hammer': 'hammer',
'transverse mercator': 'transverseMercator',
'albers usa': 'albersUsa',
'winkel tripel': 'winkel3',
'aitoff': 'aitoff',
'sinusoidal': 'sinusoidal'
};
// name of the axes
params.axesNames = ['lonaxis', 'lataxis'];
// max longitudinal angular span (EXPERIMENTAL)
params.lonaxisSpan = {
'orthographic': 180,
'azimuthal equal area': 360,
'azimuthal equidistant': 360,
'conic conformal': 180,
'gnomonic': 160,
'stereographic': 180,
'transverse mercator': 180,
'*': 360
};
// max latitudinal angular span (EXPERIMENTAL)
params.lataxisSpan = {
'conic conformal': 150,
'stereographic': 179.5,
'*': 180
};
// defaults for each scope
params.scopeDefaults = {
world: {
lonaxisRange: [-180, 180],
lataxisRange: [-90, 90],
projType: 'equirectangular',
projRotate: [0, 0, 0]
},
usa: {
lonaxisRange: [-180, -50],
lataxisRange: [15, 80],
projType: 'albers usa'
},
europe: {
lonaxisRange: [-30, 60],
lataxisRange: [30, 80],
projType: 'conic conformal',
projRotate: [15, 0, 0],
projParallels: [0, 60]
},
asia: {
lonaxisRange: [22, 160],
lataxisRange: [-15, 55],
projType: 'mercator',
projRotate: [0, 0, 0]
},
africa: {
lonaxisRange: [-30, 60],
lataxisRange: [-40, 40],
projType: 'mercator',
projRotate: [0, 0, 0]
},
'north america': {
lonaxisRange: [-180, -45],
lataxisRange: [5, 85],
projType: 'conic conformal',
projRotate: [-100, 0, 0],
projParallels: [29.5, 45.5]
},
'south america': {
lonaxisRange: [-100, -30],
lataxisRange: [-60, 15],
projType: 'mercator',
projRotate: [0, 0, 0]
}
};
// angular pad to avoid rounding error around clip angles
params.clipPad = 1e-3;
// map projection precision
params.precision = 0.1;
// default land and water fill colors
params.landColor = '#F0DC82';
params.waterColor = '#3399FF';
// locationmode to layer name
params.locationmodeToLayer = {
'ISO-3': 'countries',
'USA-states': 'subunits',
'country names': 'countries'
};
// SVG element for a sphere (use to frame maps)
params.sphereSVG = {type: 'Sphere'};
// N.B. base layer names must be the same as in the topojson files
// base layer with a fill color
params.fillLayers = ['ocean', 'land', 'lakes'];
// base layer with a only a line color
params.lineLayers = ['subunits', 'countries', 'coastlines', 'rivers', 'frame'];
// all base layers - in order
params.baseLayers = [
'ocean', 'land', 'lakes',
'subunits', 'countries', 'coastlines', 'rivers',
'lataxis', 'lonaxis',
'frame'
];
params.layerNameToAdjective = {
ocean: 'ocean',
land: 'land',
lakes: 'lake',
subunits: 'subunit',
countries: 'country',
coastlines: 'coastline',
rivers: 'river',
frame: 'frame'
};
// base layers drawn over choropleth
params.baseLayersOverChoropleth = ['rivers', 'lakes'];
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/* global PlotlyGeoAssets:false */
var d3 = require('d3');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var Plots = require('../plots');
var Axes = require('../cartesian/axes');
var Fx = require('../cartesian/graph_interact');
var addProjectionsToD3 = require('./projections');
var createGeoScale = require('./set_scale');
var createGeoZoom = require('./zoom');
var createGeoZoomReset = require('./zoom_reset');
var constants = require('./constants');
var topojsonUtils = require('../../lib/topojson_utils');
var topojsonFeature = require('topojson-client').feature;
// add a few projection types to d3.geo
addProjectionsToD3(d3);
function Geo(options) {
this.id = options.id;
this.graphDiv = options.graphDiv;
this.container = options.container;
this.topojsonURL = options.topojsonURL;
this.topojsonName = null;
this.topojson = null;
this.projectionType = null;
this.projection = null;
this.clipAngle = null;
this.setScale = null;
this.path = null;
this.zoom = null;
this.zoomReset = null;
this.makeFramework();
this.traceHash = {};
}
module.exports = Geo;
var proto = Geo.prototype;
proto.plot = function(geoCalcData, fullLayout, promises) {
var _this = this,
geoLayout = fullLayout[_this.id],
graphSize = fullLayout._size;
var topojsonNameNew, topojsonPath;
// N.B. 'geoLayout' is unambiguous, no need for 'user' geo layout here
// TODO don't reset projection on all graph edits
_this.projection = null;
_this.setScale = createGeoScale(geoLayout, graphSize);
_this.makeProjection(geoLayout);
_this.makePath();
_this.adjustLayout(geoLayout, graphSize);
_this.zoom = createGeoZoom(_this, geoLayout);
_this.zoomReset = createGeoZoomReset(_this, geoLayout);
_this.mockAxis = createMockAxis(fullLayout);
_this.framework
.call(_this.zoom)
.on('dblclick.zoom', _this.zoomReset);
_this.framework.on('mousemove', function() {
var mouse = d3.mouse(this),
lonlat = _this.projection.invert(mouse);
if(!lonlat || isNaN(lonlat[0]) || isNaN(lonlat[1])) return;
var evt = d3.event;
evt.xpx = mouse[0];
evt.ypx = mouse[1];
_this.xaxis.c2p = function() { return mouse[0]; };
_this.xaxis.p2c = function() { return lonlat[0]; };
_this.yaxis.c2p = function() { return mouse[1]; };
_this.yaxis.p2c = function() { return lonlat[1]; };
Fx.hover(_this.graphDiv, evt, _this.id);
});
_this.framework.on('mouseout', function() {
Fx.loneUnhover(fullLayout._toppaper);
});
_this.framework.on('click', function() {
Fx.click(_this.graphDiv, d3.event);
});
topojsonNameNew = topojsonUtils.getTopojsonName(geoLayout);
if(_this.topojson === null || topojsonNameNew !== _this.topojsonName) {
_this.topojsonName = topojsonNameNew;
if(PlotlyGeoAssets.topojson[_this.topojsonName] !== undefined) {
_this.topojson = PlotlyGeoAssets.topojson[_this.topojsonName];
_this.onceTopojsonIsLoaded(geoCalcData, geoLayout);
}
else {
topojsonPath = topojsonUtils.getTopojsonPath(
_this.topojsonURL,
_this.topojsonName
);
promises.push(new Promise(function(resolve, reject) {
d3.json(topojsonPath, function(error, topojson) {
if(error) {
if(error.status === 404) {
reject(new Error([
'plotly.js could not find topojson file at',
topojsonPath, '.',
'Make sure the *topojsonURL* plot config option',
'is set properly.'
].join(' ')));
}
else {
reject(new Error([
'unexpected error while fetching topojson file at',
topojsonPath
].join(' ')));
}
return;
}
_this.topojson = topojson;
PlotlyGeoAssets.topojson[_this.topojsonName] = topojson;
_this.onceTopojsonIsLoaded(geoCalcData, geoLayout);
resolve();
});
}));
}
}
else _this.onceTopojsonIsLoaded(geoCalcData, geoLayout);
// TODO handle topojson-is-loading case
// to avoid making multiple request while streaming
};
proto.onceTopojsonIsLoaded = function(geoCalcData, geoLayout) {
this.drawLayout(geoLayout);
Plots.generalUpdatePerTraceModule(this, geoCalcData, geoLayout);
this.render();
};
proto.makeProjection = function(geoLayout) {
var projLayout = geoLayout.projection,
projType = projLayout.type,
isNew = this.projection === null || projType !== this.projectionType,
projection;
if(isNew) {
this.projectionType = projType;
projection = this.projection = d3.geo[constants.projNames[projType]]();
}
else projection = this.projection;
projection
.translate(projLayout._translate0)
.precision(constants.precision);
if(!geoLayout._isAlbersUsa) {
projection
.rotate(projLayout._rotate)
.center(projLayout._center);
}
if(geoLayout._clipAngle) {
this.clipAngle = geoLayout._clipAngle; // needed in proto.render
projection
.clipAngle(geoLayout._clipAngle - constants.clipPad);
}
else this.clipAngle = null; // for graph edits
if(projLayout.parallels) {
projection
.parallels(projLayout.parallels);
}
if(isNew) this.setScale(projection);
projection
.translate(projLayout._translate)
.scale(projLayout._scale);
};
proto.makePath = function() {
this.path = d3.geo.path().projection(this.projection);
};
proto.makeFramework = function() {
var fullLayout = this.graphDiv._fullLayout;
var clipId = 'clip' + fullLayout._uid + this.id;
var defGroup = fullLayout._defs.selectAll('g.clips')
.data([0]);
defGroup.enter().append('g')
.classed('clips', true);
var clipDef = this.clipDef = defGroup.selectAll('#' + clipId)
.data([0]);
clipDef.enter().append('clipPath').attr('id', clipId)
.append('rect');
var framework = this.framework = d3.select(this.container).append('g');
framework
.attr('class', 'geo ' + this.id)
.style('pointer-events', 'all')
.call(Drawing.setClipUrl, clipId);
framework.append('g')
.attr('class', 'bglayer')
.append('rect');
framework.append('g').attr('class', 'baselayer');
framework.append('g').attr('class', 'choroplethlayer');
framework.append('g').attr('class', 'baselayeroverchoropleth');
framework.append('g').attr('class', 'scattergeolayer');
// N.B. disable dblclick zoom default
framework.on('dblclick.zoom', null);
this.xaxis = { _id: 'x' };
this.yaxis = { _id: 'y' };
};
proto.adjustLayout = function(geoLayout, graphSize) {
var domain = geoLayout.domain;
var left = graphSize.l + graphSize.w * domain.x[0] + geoLayout._marginX,
top = graphSize.t + graphSize.h * (1 - domain.y[1]) + geoLayout._marginY;
Drawing.setTranslate(this.framework, left, top);
var dimsAttrs = {
x: 0,
y: 0,
width: geoLayout._width,
height: geoLayout._height
};
this.clipDef.select('rect')
.attr(dimsAttrs);
this.framework.select('.bglayer').select('rect')
.attr(dimsAttrs)
.call(Color.fill, geoLayout.bgcolor);
this.xaxis._offset = left;
this.xaxis._length = geoLayout._width;
this.yaxis._offset = top;
this.yaxis._length = geoLayout._height;
};
proto.drawTopo = function(selection, layerName, geoLayout) {
if(geoLayout['show' + layerName] !== true) return;
var topojson = this.topojson,
datum = layerName === 'frame' ?
constants.sphereSVG :
topojsonFeature(topojson, topojson.objects[layerName]);
selection.append('g')
.datum(datum)
.attr('class', layerName)
.append('path')
.attr('class', 'basepath');
};
function makeGraticule(lonaxisRange, lataxisRange, step) {
return d3.geo.graticule()
.extent([
[lonaxisRange[0], lataxisRange[0]],
[lonaxisRange[1], lataxisRange[1]]
])
.step(step);
}
proto.drawGraticule = function(selection, axisName, geoLayout) {
var axisLayout = geoLayout[axisName];
if(axisLayout.showgrid !== true) return;
var scopeDefaults = constants.scopeDefaults[geoLayout.scope],
lonaxisRange = scopeDefaults.lonaxisRange,
lataxisRange = scopeDefaults.lataxisRange,
step = axisName === 'lonaxis' ?
[axisLayout.dtick] :
[0, axisLayout.dtick],
graticule = makeGraticule(lonaxisRange, lataxisRange, step);
selection.append('g')
.datum(graticule)
.attr('class', axisName + 'graticule')
.append('path')
.attr('class', 'graticulepath');
};
proto.drawLayout = function(geoLayout) {
var gBaseLayer = this.framework.select('g.baselayer'),
baseLayers = constants.baseLayers,
axesNames = constants.axesNames,
layerName;
// TODO move to more d3-idiomatic pattern (that's work on replot)
// N.B. html('') does not work in IE11
gBaseLayer.selectAll('*').remove();
for(var i = 0; i < baseLayers.length; i++) {
layerName = baseLayers[i];
if(axesNames.indexOf(layerName) !== -1) {
this.drawGraticule(gBaseLayer, layerName, geoLayout);
}
else this.drawTopo(gBaseLayer, layerName, geoLayout);
}
this.styleLayout(geoLayout);
};
function styleFillLayer(selection, layerName, geoLayout) {
var layerAdj = constants.layerNameToAdjective[layerName];
selection.select('.' + layerName)
.selectAll('path')
.attr('stroke', 'none')
.call(Color.fill, geoLayout[layerAdj + 'color']);
}
function styleLineLayer(selection, layerName, geoLayout) {
var layerAdj = constants.layerNameToAdjective[layerName];
selection.select('.' + layerName)
.selectAll('path')
.attr('fill', 'none')
.call(Color.stroke, geoLayout[layerAdj + 'color'])
.call(Drawing.dashLine, '', geoLayout[layerAdj + 'width']);
}
function styleGraticule(selection, axisName, geoLayout) {
selection.select('.' + axisName + 'graticule')
.selectAll('path')
.attr('fill', 'none')
.call(Color.stroke, geoLayout[axisName].gridcolor)
.call(Drawing.dashLine, '', geoLayout[axisName].gridwidth);
}
proto.styleLayer = function(selection, layerName, geoLayout) {
var fillLayers = constants.fillLayers,
lineLayers = constants.lineLayers;
if(fillLayers.indexOf(layerName) !== -1) {
styleFillLayer(selection, layerName, geoLayout);
}
else if(lineLayers.indexOf(layerName) !== -1) {
styleLineLayer(selection, layerName, geoLayout);
}
};
proto.styleLayout = function(geoLayout) {
var gBaseLayer = this.framework.select('g.baselayer'),
baseLayers = constants.baseLayers,
axesNames = constants.axesNames,
layerName;
for(var i = 0; i < baseLayers.length; i++) {
layerName = baseLayers[i];
if(axesNames.indexOf(layerName) !== -1) {
styleGraticule(gBaseLayer, layerName, geoLayout);
}
else this.styleLayer(gBaseLayer, layerName, geoLayout);
}
};
proto.isLonLatOverEdges = function(lonlat) {
var clipAngle = this.clipAngle;
if(clipAngle === null) return false;
var p = this.projection.rotate(),
angle = d3.geo.distance(lonlat, [-p[0], -p[1]]),
maxAngle = clipAngle * Math.PI / 180;
return angle > maxAngle;
};
// [hot code path] (re)draw all paths which depend on the projection
proto.render = function() {
var _this = this,
framework = _this.framework,
gChoropleth = framework.select('g.choroplethlayer'),
gScatterGeo = framework.select('g.scattergeolayer'),
path = _this.path;
function translatePoints(d) {
var lonlatPx = _this.projection(d.lonlat);
if(!lonlatPx) return null;
return 'translate(' + lonlatPx[0] + ',' + lonlatPx[1] + ')';
}
// hide paths over edges of clipped projections
function hideShowPoints(d) {
return _this.isLonLatOverEdges(d.lonlat) ? '0' : '1.0';
}
framework.selectAll('path.basepath').attr('d', path);
framework.selectAll('path.graticulepath').attr('d', path);
gChoropleth.selectAll('path.choroplethlocation').attr('d', path);
gChoropleth.selectAll('path.basepath').attr('d', path);
gScatterGeo.selectAll('path.js-line').attr('d', path);
if(_this.clipAngle !== null) {
gScatterGeo.selectAll('path.point')
.style('opacity', hideShowPoints)
.attr('transform', translatePoints);
gScatterGeo.selectAll('text')
.style('opacity', hideShowPoints)
.attr('transform', translatePoints);
}
else {
gScatterGeo.selectAll('path.point')
.attr('transform', translatePoints);
gScatterGeo.selectAll('text')
.attr('transform', translatePoints);
}
};
// create a mock axis used to format hover text
function createMockAxis(fullLayout) {
var mockAxis = {
type: 'linear',
showexponent: 'all',
exponentformat: Axes.layoutAttributes.exponentformat.dflt
};
Axes.setConvert(mockAxis, fullLayout);
return mockAxis;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 20 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*
* Generated by https://github.com/etpinard/d3-geo-projection-picker
*
* which is hand-picks projection from https://github.com/d3/d3-geo-projection
*
* into a CommonJS require-able module.
*/
'use strict';
/* eslint-disable */
function addProjectionsToD3(d3) {
d3.geo.project = function(object, projection) {
var stream = projection.stream;
if (!stream) throw new Error("not yet supported");
return (object && d3_geo_projectObjectType.hasOwnProperty(object.type) ? d3_geo_projectObjectType[object.type] : d3_geo_projectGeometry)(object, stream);
};
function d3_geo_projectFeature(object, stream) {
return {
type: "Feature",
id: object.id,
properties: object.properties,
geometry: d3_geo_projectGeometry(object.geometry, stream)
};
}
function d3_geo_projectGeometry(geometry, stream) {
if (!geometry) return null;
if (geometry.type === "GeometryCollection") return {
type: "GeometryCollection",
geometries: object.geometries.map(function(geometry) {
return d3_geo_projectGeometry(geometry, stream);
})
};
if (!d3_geo_projectGeometryType.hasOwnProperty(geometry.type)) return null;
var sink = d3_geo_projectGeometryType[geometry.type];
d3.geo.stream(geometry, stream(sink));
return sink.result();
}
var d3_geo_projectObjectType = {
Feature: d3_geo_projectFeature,
FeatureCollection: function(object, stream) {
return {
type: "FeatureCollection",
features: object.features.map(function(feature) {
return d3_geo_projectFeature(feature, stream);
})
};
}
};
var d3_geo_projectPoints = [], d3_geo_projectLines = [];
var d3_geo_projectPoint = {
point: function(x, y) {
d3_geo_projectPoints.push([ x, y ]);
},
result: function() {
var result = !d3_geo_projectPoints.length ? null : d3_geo_projectPoints.length < 2 ? {
type: "Point",
coordinates: d3_geo_projectPoints[0]
} : {
type: "MultiPoint",
coordinates: d3_geo_projectPoints
};
d3_geo_projectPoints = [];
return result;
}
};
var d3_geo_projectLine = {
lineStart: d3_geo_projectNoop,
point: function(x, y) {
d3_geo_projectPoints.push([ x, y ]);
},
lineEnd: function() {
if (d3_geo_projectPoints.length) d3_geo_projectLines.push(d3_geo_projectPoints),
d3_geo_projectPoints = [];
},
result: function() {
var result = !d3_geo_projectLines.length ? null : d3_geo_projectLines.length < 2 ? {
type: "LineString",
coordinates: d3_geo_projectLines[0]
} : {
type: "MultiLineString",
coordinates: d3_geo_projectLines
};
d3_geo_projectLines = [];
return result;
}
};
var d3_geo_projectPolygon = {
polygonStart: d3_geo_projectNoop,
lineStart: d3_geo_projectNoop,
point: function(x, y) {
d3_geo_projectPoints.push([ x, y ]);
},
lineEnd: function() {
var n = d3_geo_projectPoints.length;
if (n) {
do d3_geo_projectPoints.push(d3_geo_projectPoints[0].slice()); while (++n < 4);
d3_geo_projectLines.push(d3_geo_projectPoints), d3_geo_projectPoints = [];
}
},
polygonEnd: d3_geo_projectNoop,
result: function() {
if (!d3_geo_projectLines.length) return null;
var polygons = [], holes = [];
d3_geo_projectLines.forEach(function(ring) {
if (d3_geo_projectClockwise(ring)) polygons.push([ ring ]); else holes.push(ring);
});
holes.forEach(function(hole) {
var point = hole[0];
polygons.some(function(polygon) {
if (d3_geo_projectContains(polygon[0], point)) {
polygon.push(hole);
return true;
}
}) || polygons.push([ hole ]);
});
d3_geo_projectLines = [];
return !polygons.length ? null : polygons.length > 1 ? {
type: "MultiPolygon",
coordinates: polygons
} : {
type: "Polygon",
coordinates: polygons[0]
};
}
};
var d3_geo_projectGeometryType = {
Point: d3_geo_projectPoint,
MultiPoint: d3_geo_projectPoint,
LineString: d3_geo_projectLine,
MultiLineString: d3_geo_projectLine,
Polygon: d3_geo_projectPolygon,
MultiPolygon: d3_geo_projectPolygon,
Sphere: d3_geo_projectPolygon
};
function d3_geo_projectNoop() {}
function d3_geo_projectClockwise(ring) {
if ((n = ring.length) < 4) return false;
var i = 0, n, area = ring[n - 1][1] * ring[0][0] - ring[n - 1][0] * ring[0][1];
while (++i < n) area += ring[i - 1][1] * ring[i][0] - ring[i - 1][0] * ring[i][1];
return area <= 0;
}
function d3_geo_projectContains(ring, point) {
var x = point[0], y = point[1], contains = false;
for (var i = 0, n = ring.length, j = n - 1; i < n; j = i++) {
var pi = ring[i], xi = pi[0], yi = pi[1], pj = ring[j], xj = pj[0], yj = pj[1];
if (yi > y ^ yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) contains = !contains;
}
return contains;
}
var ε = 1e-6, ε2 = ε * ε, π = Math.PI, halfπ = π / 2, sqrtπ = Math.sqrt(π), radians = π / 180, degrees = 180 / π;
function sinci(x) {
return x ? x / Math.sin(x) : 1;
}
function sgn(x) {
return x > 0 ? 1 : x < 0 ? -1 : 0;
}
function asin(x) {
return x > 1 ? halfπ : x < -1 ? -halfπ : Math.asin(x);
}
function acos(x) {
return x > 1 ? 0 : x < -1 ? π : Math.acos(x);
}
function asqrt(x) {
return x > 0 ? Math.sqrt(x) : 0;
}
var projection = d3.geo.projection, projectionMutator = d3.geo.projectionMutator;
d3.geo.interrupt = function(project) {
var lobes = [ [ [ [ -π, 0 ], [ 0, halfπ ], [ π, 0 ] ] ], [ [ [ -π, 0 ], [ 0, -halfπ ], [ π, 0 ] ] ] ];
var bounds;
function forward(λ, φ) {
var sign = φ < 0 ? -1 : +1, hemilobes = lobes[+(φ < 0)];
for (var i = 0, n = hemilobes.length - 1; i < n && λ > hemilobes[i][2][0]; ++i) ;
var coordinates = project(λ - hemilobes[i][1][0], φ);
coordinates[0] += project(hemilobes[i][1][0], sign * φ > sign * hemilobes[i][0][1] ? hemilobes[i][0][1] : φ)[0];
return coordinates;
}
function reset() {
bounds = lobes.map(function(hemilobes) {
return hemilobes.map(function(lobe) {
var x0 = project(lobe[0][0], lobe[0][1])[0], x1 = project(lobe[2][0], lobe[2][1])[0], y0 = project(lobe[1][0], lobe[0][1])[1], y1 = project(lobe[1][0], lobe[1][1])[1], t;
if (y0 > y1) t = y0, y0 = y1, y1 = t;
return [ [ x0, y0 ], [ x1, y1 ] ];
});
});
}
if (project.invert) forward.invert = function(x, y) {
var hemibounds = bounds[+(y < 0)], hemilobes = lobes[+(y < 0)];
for (var i = 0, n = hemibounds.length; i < n; ++i) {
var b = hemibounds[i];
if (b[0][0] <= x && x < b[1][0] && b[0][1] <= y && y < b[1][1]) {
var coordinates = project.invert(x - project(hemilobes[i][1][0], 0)[0], y);
coordinates[0] += hemilobes[i][1][0];
return pointEqual(forward(coordinates[0], coordinates[1]), [ x, y ]) ? coordinates : null;
}
}
};
var projection = d3.geo.projection(forward), stream_ = projection.stream;
projection.stream = function(stream) {
var rotate = projection.rotate(), rotateStream = stream_(stream), sphereStream = (projection.rotate([ 0, 0 ]),
stream_(stream));
projection.rotate(rotate);
rotateStream.sphere = function() {
d3.geo.stream(sphere(), sphereStream);
};
return rotateStream;
};
projection.lobes = function(_) {
if (!arguments.length) return lobes.map(function(lobes) {
return lobes.map(function(lobe) {
return [ [ lobe[0][0] * 180 / π, lobe[0][1] * 180 / π ], [ lobe[1][0] * 180 / π, lobe[1][1] * 180 / π ], [ lobe[2][0] * 180 / π, lobe[2][1] * 180 / π ] ];
});
});
lobes = _.map(function(lobes) {
return lobes.map(function(lobe) {
return [ [ lobe[0][0] * π / 180, lobe[0][1] * π / 180 ], [ lobe[1][0] * π / 180, lobe[1][1] * π / 180 ], [ lobe[2][0] * π / 180, lobe[2][1] * π / 180 ] ];
});
});
reset();
return projection;
};
function sphere() {
var ε = 1e-6, coordinates = [];
for (var i = 0, n = lobes[0].length; i < n; ++i) {
var lobe = lobes[0][i], λ0 = lobe[0][0] * 180 / π, φ0 = lobe[0][1] * 180 / π, φ1 = lobe[1][1] * 180 / π, λ2 = lobe[2][0] * 180 / π, φ2 = lobe[2][1] * 180 / π;
coordinates.push(resample([ [ λ0 + ε, φ0 + ε ], [ λ0 + ε, φ1 - ε ], [ λ2 - ε, φ1 - ε ], [ λ2 - ε, φ2 + ε ] ], 30));
}
for (var i = lobes[1].length - 1; i >= 0; --i) {
var lobe = lobes[1][i], λ0 = lobe[0][0] * 180 / π, φ0 = lobe[0][1] * 180 / π, φ1 = lobe[1][1] * 180 / π, λ2 = lobe[2][0] * 180 / π, φ2 = lobe[2][1] * 180 / π;
coordinates.push(resample([ [ λ2 - ε, φ2 - ε ], [ λ2 - ε, φ1 + ε ], [ λ0 + ε, φ1 + ε ], [ λ0 + ε, φ0 - ε ] ], 30));
}
return {
type: "Polygon",
coordinates: [ d3.merge(coordinates) ]
};
}
function resample(coordinates, m) {
var i = -1, n = coordinates.length, p0 = coordinates[0], p1, dx, dy, resampled = [];
while (++i < n) {
p1 = coordinates[i];
dx = (p1[0] - p0[0]) / m;
dy = (p1[1] - p0[1]) / m;
for (var j = 0; j < m; ++j) resampled.push([ p0[0] + j * dx, p0[1] + j * dy ]);
p0 = p1;
}
resampled.push(p1);
return resampled;
}
function pointEqual(a, b) {
return Math.abs(a[0] - b[0]) < ε && Math.abs(a[1] - b[1]) < ε;
}
return projection;
};
function eckert4(λ, φ) {
var k = (2 + halfπ) * Math.sin(φ);
φ /= 2;
for (var i = 0, δ = Infinity; i < 10 && Math.abs(δ) > ε; i++) {
var cosφ = Math.cos(φ);
φ -= δ = (φ + Math.sin(φ) * (cosφ + 2) - k) / (2 * cosφ * (1 + cosφ));
}
return [ 2 / Math.sqrt(π * (4 + π)) * λ * (1 + Math.cos(φ)), 2 * Math.sqrt(π / (4 + π)) * Math.sin(φ) ];
}
eckert4.invert = function(x, y) {
var A = .5 * y * Math.sqrt((4 + π) / π), k = asin(A), c = Math.cos(k);
return [ x / (2 / Math.sqrt(π * (4 + π)) * (1 + c)), asin((k + A * (c + 2)) / (2 + halfπ)) ];
};
(d3.geo.eckert4 = function() {
return projection(eckert4);
}).raw = eckert4;
var hammerAzimuthalEqualArea = d3.geo.azimuthalEqualArea.raw;
function hammer(A, B) {
if (arguments.length < 2) B = A;
if (B === 1) return hammerAzimuthalEqualArea;
if (B === Infinity) return hammerQuarticAuthalic;
function forward(λ, φ) {
var coordinates = hammerAzimuthalEqualArea(λ / B, φ);
coordinates[0] *= A;
return coordinates;
}
forward.invert = function(x, y) {
var coordinates = hammerAzimuthalEqualArea.invert(x / A, y);
coordinates[0] *= B;
return coordinates;
};
return forward;
}
function hammerProjection() {
var B = 2, m = projectionMutator(hammer), p = m(B);
p.coefficient = function(_) {
if (!arguments.length) return B;
return m(B = +_);
};
return p;
}
function hammerQuarticAuthalic(λ, φ) {
return [ λ * Math.cos(φ) / Math.cos(φ /= 2), 2 * Math.sin(φ) ];
}
hammerQuarticAuthalic.invert = function(x, y) {
var φ = 2 * asin(y / 2);
return [ x * Math.cos(φ / 2) / Math.cos(φ), φ ];
};
(d3.geo.hammer = hammerProjection).raw = hammer;
function kavrayskiy7(λ, φ) {
return [ 3 * λ / (2 * π) * Math.sqrt(π * π / 3 - φ * φ), φ ];
}
kavrayskiy7.invert = function(x, y) {
return [ 2 / 3 * π * x / Math.sqrt(π * π / 3 - y * y), y ];
};
(d3.geo.kavrayskiy7 = function() {
return projection(kavrayskiy7);
}).raw = kavrayskiy7;
function miller(λ, φ) {
return [ λ, 1.25 * Math.log(Math.tan(π / 4 + .4 * φ)) ];
}
miller.invert = function(x, y) {
return [ x, 2.5 * Math.atan(Math.exp(.8 * y)) - .625 * π ];
};
(d3.geo.miller = function() {
return projection(miller);
}).raw = miller;
function mollweideBromleyθ(Cp) {
return function(θ) {
var Cpsinθ = Cp * Math.sin(θ), i = 30, δ;
do θ -= δ = (θ + Math.sin(θ) - Cpsinθ) / (1 + Math.cos(θ)); while (Math.abs(δ) > ε && --i > 0);
return θ / 2;
};
}
function mollweideBromley(Cx, Cy, Cp) {
var θ = mollweideBromleyθ(Cp);
function forward(λ, φ) {
return [ Cx * λ * Math.cos(φ = θ(φ)), Cy * Math.sin(φ) ];
}
forward.invert = function(x, y) {
var θ = asin(y / Cy);
return [ x / (Cx * Math.cos(θ)), asin((2 * θ + Math.sin(2 * θ)) / Cp) ];
};
return forward;
}
var mollweideθ = mollweideBromleyθ(π), mollweide = mollweideBromley(Math.SQRT2 / halfπ, Math.SQRT2, π);
(d3.geo.mollweide = function() {
return projection(mollweide);
}).raw = mollweide;
function naturalEarth(λ, φ) {
var φ2 = φ * φ, φ4 = φ2 * φ2;
return [ λ * (.8707 - .131979 * φ2 + φ4 * (-.013791 + φ4 * (.003971 * φ2 - .001529 * φ4))), φ * (1.007226 + φ2 * (.015085 + φ4 * (-.044475 + .028874 * φ2 - .005916 * φ4))) ];
}
naturalEarth.invert = function(x, y) {
var φ = y, i = 25, δ;
do {
var φ2 = φ * φ, φ4 = φ2 * φ2;
φ -= δ = (φ * (1.007226 + φ2 * (.015085 + φ4 * (-.044475 + .028874 * φ2 - .005916 * φ4))) - y) / (1.007226 + φ2 * (.015085 * 3 + φ4 * (-.044475 * 7 + .028874 * 9 * φ2 - .005916 * 11 * φ4)));
} while (Math.abs(δ) > ε && --i > 0);
return [ x / (.8707 + (φ2 = φ * φ) * (-.131979 + φ2 * (-.013791 + φ2 * φ2 * φ2 * (.003971 - .001529 * φ2)))), φ ];
};
(d3.geo.naturalEarth = function() {
return projection(naturalEarth);
}).raw = naturalEarth;
var robinsonConstants = [ [ .9986, -.062 ], [ 1, 0 ], [ .9986, .062 ], [ .9954, .124 ], [ .99, .186 ], [ .9822, .248 ], [ .973, .31 ], [ .96, .372 ], [ .9427, .434 ], [ .9216, .4958 ], [ .8962, .5571 ], [ .8679, .6176 ], [ .835, .6769 ], [ .7986, .7346 ], [ .7597, .7903 ], [ .7186, .8435 ], [ .6732, .8936 ], [ .6213, .9394 ], [ .5722, .9761 ], [ .5322, 1 ] ];
robinsonConstants.forEach(function(d) {
d[1] *= 1.0144;
});
function robinson(λ, φ) {
var i = Math.min(18, Math.abs(φ) * 36 / π), i0 = Math.floor(i), di = i - i0, ax = (k = robinsonConstants[i0])[0], ay = k[1], bx = (k = robinsonConstants[++i0])[0], by = k[1], cx = (k = robinsonConstants[Math.min(19, ++i0)])[0], cy = k[1], k;
return [ λ * (bx + di * (cx - ax) / 2 + di * di * (cx - 2 * bx + ax) / 2), (φ > 0 ? halfπ : -halfπ) * (by + di * (cy - ay) / 2 + di * di * (cy - 2 * by + ay) / 2) ];
}
robinson.invert = function(x, y) {
var yy = y / halfπ, φ = yy * 90, i = Math.min(18, Math.abs(φ / 5)), i0 = Math.max(0, Math.floor(i));
do {
var ay = robinsonConstants[i0][1], by = robinsonConstants[i0 + 1][1], cy = robinsonConstants[Math.min(19, i0 + 2)][1], u = cy - ay, v = cy - 2 * by + ay, t = 2 * (Math.abs(yy) - by) / u, c = v / u, di = t * (1 - c * t * (1 - 2 * c * t));
if (di >= 0 || i0 === 1) {
φ = (y >= 0 ? 5 : -5) * (di + i);
var j = 50, δ;
do {
i = Math.min(18, Math.abs(φ) / 5);
i0 = Math.floor(i);
di = i - i0;
ay = robinsonConstants[i0][1];
by = robinsonConstants[i0 + 1][1];
cy = robinsonConstants[Math.min(19, i0 + 2)][1];
φ -= (δ = (y >= 0 ? halfπ : -halfπ) * (by + di * (cy - ay) / 2 + di * di * (cy - 2 * by + ay) / 2) - y) * degrees;
} while (Math.abs(δ) > ε2 && --j > 0);
break;
}
} while (--i0 >= 0);
var ax = robinsonConstants[i0][0], bx = robinsonConstants[i0 + 1][0], cx = robinsonConstants[Math.min(19, i0 + 2)][0];
return [ x / (bx + di * (cx - ax) / 2 + di * di * (cx - 2 * bx + ax) / 2), φ * radians ];
};
(d3.geo.robinson = function() {
return projection(robinson);
}).raw = robinson;
function sinusoidal(λ, φ) {
return [ λ * Math.cos(φ), φ ];
}
sinusoidal.invert = function(x, y) {
return [ x / Math.cos(y), y ];
};
(d3.geo.sinusoidal = function() {
return projection(sinusoidal);
}).raw = sinusoidal;
function aitoff(λ, φ) {
var cosφ = Math.cos(φ), sinciα = sinci(acos(cosφ * Math.cos(λ /= 2)));
return [ 2 * cosφ * Math.sin(λ) * sinciα, Math.sin(φ) * sinciα ];
}
aitoff.invert = function(x, y) {
if (x * x + 4 * y * y > π * π + ε) return;
var λ = x, φ = y, i = 25;
do {
var sinλ = Math.sin(λ), sinλ_2 = Math.sin(λ / 2), cosλ_2 = Math.cos(λ / 2), sinφ = Math.sin(φ), cosφ = Math.cos(φ), sin_2φ = Math.sin(2 * φ), sin2φ = sinφ * sinφ, cos2φ = cosφ * cosφ, sin2λ_2 = sinλ_2 * sinλ_2, C = 1 - cos2φ * cosλ_2 * cosλ_2, E = C ? acos(cosφ * cosλ_2) * Math.sqrt(F = 1 / C) : F = 0, F, fx = 2 * E * cosφ * sinλ_2 - x, fy = E * sinφ - y, δxδλ = F * (cos2φ * sin2λ_2 + E * cosφ * cosλ_2 * sin2φ), δxδφ = F * (.5 * sinλ * sin_2φ - E * 2 * sinφ * sinλ_2), δyδλ = F * .25 * (sin_2φ * sinλ_2 - E * sinφ * cos2φ * sinλ), δyδφ = F * (sin2φ * cosλ_2 + E * sin2λ_2 * cosφ), denominator = δxδφ * δyδλ - δyδφ * δxδλ;
if (!denominator) break;
var δλ = (fy * δxδφ - fx * δyδφ) / denominator, δφ = (fx * δyδλ - fy * δxδλ) / denominator;
λ -= δλ, φ -= δφ;
} while ((Math.abs(δλ) > ε || Math.abs(δφ) > ε) && --i > 0);
return [ λ, φ ];
};
(d3.geo.aitoff = function() {
return projection(aitoff);
}).raw = aitoff;
function winkel3(λ, φ) {
var coordinates = aitoff(λ, φ);
return [ (coordinates[0] + λ / halfπ) / 2, (coordinates[1] + φ) / 2 ];
}
winkel3.invert = function(x, y) {
var λ = x, φ = y, i = 25;
do {
var cosφ = Math.cos(φ), sinφ = Math.sin(φ), sin_2φ = Math.sin(2 * φ), sin2φ = sinφ * sinφ, cos2φ = cosφ * cosφ, sinλ = Math.sin(λ), cosλ_2 = Math.cos(λ / 2), sinλ_2 = Math.sin(λ / 2), sin2λ_2 = sinλ_2 * sinλ_2, C = 1 - cos2φ * cosλ_2 * cosλ_2, E = C ? acos(cosφ * cosλ_2) * Math.sqrt(F = 1 / C) : F = 0, F, fx = .5 * (2 * E * cosφ * sinλ_2 + λ / halfπ) - x, fy = .5 * (E * sinφ + φ) - y, δxδλ = .5 * F * (cos2φ * sin2λ_2 + E * cosφ * cosλ_2 * sin2φ) + .5 / halfπ, δxδφ = F * (sinλ * sin_2φ / 4 - E * sinφ * sinλ_2), δyδλ = .125 * F * (sin_2φ * sinλ_2 - E * sinφ * cos2φ * sinλ), δyδφ = .5 * F * (sin2φ * cosλ_2 + E * sin2λ_2 * cosφ) + .5, denominator = δxδφ * δyδλ - δyδφ * δxδλ, δλ = (fy * δxδφ - fx * δyδφ) / denominator, δφ = (fx * δyδλ - fy * δxδλ) / denominator;
λ -= δλ, φ -= δφ;
} while ((Math.abs(δλ) > ε || Math.abs(δφ) > ε) && --i > 0);
return [ λ, φ ];
};
(d3.geo.winkel3 = function() {
return projection(winkel3);
}).raw = winkel3;
}
module.exports = addProjectionsToD3;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var clipPad = require('./constants').clipPad;
function createGeoScale(geoLayout, graphSize) {
var projLayout = geoLayout.projection,
lonaxisLayout = geoLayout.lonaxis,
lataxisLayout = geoLayout.lataxis,
geoDomain = geoLayout.domain,
frameWidth = geoLayout.framewidth || 0;
// width & height the geo div
var geoWidth = graphSize.w * (geoDomain.x[1] - geoDomain.x[0]),
geoHeight = graphSize.h * (geoDomain.y[1] - geoDomain.y[0]);
// add padding around range to avoid aliasing
var lon0 = lonaxisLayout.range[0] + clipPad,
lon1 = lonaxisLayout.range[1] - clipPad,
lat0 = lataxisLayout.range[0] + clipPad,
lat1 = lataxisLayout.range[1] - clipPad,
lonfull0 = lonaxisLayout._fullRange[0] + clipPad,
lonfull1 = lonaxisLayout._fullRange[1] - clipPad,
latfull0 = lataxisLayout._fullRange[0] + clipPad,
latfull1 = lataxisLayout._fullRange[1] - clipPad;
// initial translation (makes the math easier)
projLayout._translate0 = [
graphSize.l + geoWidth / 2, graphSize.t + geoHeight / 2
];
// center of the projection is given by
// the lon/lat ranges and the rotate angle
var dlon = lon1 - lon0,
dlat = lat1 - lat0,
c0 = [lon0 + dlon / 2, lat0 + dlat / 2],
r = projLayout._rotate;
projLayout._center = [c0[0] + r[0], c0[1] + r[1]];
// needs a initial projection; it is called from makeProjection
var setScale = function(projection) {
var scale0 = projection.scale(),
translate0 = projLayout._translate0,
rangeBox = makeRangeBox(lon0, lat0, lon1, lat1),
fullRangeBox = makeRangeBox(lonfull0, latfull0, lonfull1, latfull1);
var scale, translate, bounds, fullBounds;
// Inspired by: http://stackoverflow.com/a/14654988/4068492
// using the path determine the bounds of the current map and use
// these to determine better values for the scale and translation
function getScale(bounds) {
return Math.min(
scale0 * geoWidth / (bounds[1][0] - bounds[0][0]),
scale0 * geoHeight / (bounds[1][1] - bounds[0][1])
);
}
// scale projection given how range box get deformed
// by the projection
bounds = getBounds(projection, rangeBox);
scale = getScale(bounds);
// similarly, get scale at full range
fullBounds = getBounds(projection, fullRangeBox);
projLayout._fullScale = getScale(fullBounds);
projection.scale(scale);
// translate the projection so that the top-left corner
// of the range box is at the top-left corner of the viewbox
bounds = getBounds(projection, rangeBox);
translate = [
translate0[0] - bounds[0][0] + frameWidth,
translate0[1] - bounds[0][1] + frameWidth
];
projLayout._translate = translate;
projection.translate(translate);
// clip regions out of the range box
// (these are clipping along horizontal/vertical lines)
bounds = getBounds(projection, rangeBox);
if(!geoLayout._isAlbersUsa) projection.clipExtent(bounds);
// adjust scale one more time with the 'scale' attribute
scale = projLayout.scale * scale;
// set projection scale and save it
projLayout._scale = scale;
// save the effective width & height of the geo framework
geoLayout._width = Math.round(bounds[1][0]) + frameWidth;
geoLayout._height = Math.round(bounds[1][1]) + frameWidth;
// save the margin length induced by the map scaling
geoLayout._marginX = (geoWidth - Math.round(bounds[1][0])) / 2;
geoLayout._marginY = (geoHeight - Math.round(bounds[1][1])) / 2;
};
return setScale;
}
module.exports = createGeoScale;
// polygon GeoJSON corresponding to lon/lat range box
// with well-defined direction
function makeRangeBox(lon0, lat0, lon1, lat1) {
var dlon4 = (lon1 - lon0) / 4;
// TODO is this enough to handle ALL cases?
// -- this makes scaling less precise than using d3.geo.graticule
// as great circles can overshoot the boundary
// (that's not a big deal I think)
return {
type: 'Polygon',
coordinates: [
[ [lon0, lat0],
[lon0, lat1],
[lon0 + dlon4, lat1],
[lon0 + 2 * dlon4, lat1],
[lon0 + 3 * dlon4, lat1],
[lon1, lat1],
[lon1, lat0],
[lon1 - dlon4, lat0],
[lon1 - 2 * dlon4, lat0],
[lon1 - 3 * dlon4, lat0],
[lon0, lat0] ]
]
};
}
// bounds array [[top, left], [bottom, right]]
// of the lon/lat range box
function getBounds(projection, rangeBox) {
return d3.geo.path().projection(projection).bounds(rangeBox);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var radians = Math.PI / 180,
degrees = 180 / Math.PI,
zoomstartStyle = { cursor: 'pointer' },
zoomendStyle = { cursor: 'auto' };
function createGeoZoom(geo, geoLayout) {
var zoomConstructor;
if(geoLayout._isScoped) zoomConstructor = zoomScoped;
else if(geoLayout._clipAngle) zoomConstructor = zoomClipped;
else zoomConstructor = zoomNonClipped;
// TODO add a conic-specific zoom
return zoomConstructor(geo, geoLayout.projection);
}
module.exports = createGeoZoom;
// common to all zoom types
function initZoom(projection, projLayout) {
var fullScale = projLayout._fullScale;
return d3.behavior.zoom()
.translate(projection.translate())
.scale(projection.scale())
.scaleExtent([0.5 * fullScale, 100 * fullScale]);
}
// zoom for scoped projections
function zoomScoped(geo, projLayout) {
var projection = geo.projection,
zoom = initZoom(projection, projLayout);
function handleZoomstart() {
d3.select(this).style(zoomstartStyle);
}
function handleZoom() {
projection
.scale(d3.event.scale)
.translate(d3.event.translate);
geo.render();
}
function handleZoomend() {
d3.select(this).style(zoomendStyle);
}
zoom
.on('zoomstart', handleZoomstart)
.on('zoom', handleZoom)
.on('zoomend', handleZoomend);
return zoom;
}
// zoom for non-clipped projections
function zoomNonClipped(geo, projLayout) {
var projection = geo.projection,
zoom = initZoom(projection, projLayout);
var INSIDETOLORANCEPXS = 2;
var mouse0, rotate0, translate0, lastRotate, zoomPoint,
mouse1, rotate1, point1;
function position(x) { return projection.invert(x); }
function outside(x) {
var pt = projection(position(x));
return (Math.abs(pt[0] - x[0]) > INSIDETOLORANCEPXS ||
Math.abs(pt[1] - x[1]) > INSIDETOLORANCEPXS);
}
function handleZoomstart() {
d3.select(this).style(zoomstartStyle);
mouse0 = d3.mouse(this);
rotate0 = projection.rotate();
translate0 = projection.translate();
lastRotate = rotate0;
zoomPoint = position(mouse0);
}
function handleZoom() {
mouse1 = d3.mouse(this);
if(outside(mouse0)) {
zoom.scale(projection.scale());
zoom.translate(projection.translate());
return;
}
projection.scale(d3.event.scale);
projection.translate([translate0[0], d3.event.translate[1]]);
if(!zoomPoint) {
mouse0 = mouse1;
zoomPoint = position(mouse0);
}
else if(position(mouse1)) {
point1 = position(mouse1);
rotate1 = [lastRotate[0] + (point1[0] - zoomPoint[0]), rotate0[1], rotate0[2]];
projection.rotate(rotate1);
lastRotate = rotate1;
}
geo.render();
}
function handleZoomend() {
d3.select(this).style(zoomendStyle);
// or something like
// http://www.jasondavies.com/maps/gilbert/
// ... a little harder with multiple base layers
}
zoom
.on('zoomstart', handleZoomstart)
.on('zoom', handleZoom)
.on('zoomend', handleZoomend);
return zoom;
}
// zoom for clipped projections
// inspired by https://www.jasondavies.com/maps/d3.geo.zoom.js
function zoomClipped(geo, projLayout) {
var projection = geo.projection,
view = {r: projection.rotate(), k: projection.scale()},
zoom = initZoom(projection, projLayout),
event = d3_eventDispatch(zoom, 'zoomstart', 'zoom', 'zoomend'),
zooming = 0,
zoomOn = zoom.on;
var zoomPoint;
zoom.on('zoomstart', function() {
d3.select(this).style(zoomstartStyle);
var mouse0 = d3.mouse(this),
rotate0 = projection.rotate(),
lastRotate = rotate0,
translate0 = projection.translate(),
q = quaternionFromEuler(rotate0);
zoomPoint = position(projection, mouse0);
zoomOn.call(zoom, 'zoom', function() {
var mouse1 = d3.mouse(this);
projection.scale(view.k = d3.event.scale);
if(!zoomPoint) {
// if no zoomPoint, the mouse wasn't over the actual geography yet
// maybe this point is the start... we'll find out next time!
mouse0 = mouse1;
zoomPoint = position(projection, mouse0);
}
// check if the point is on the map
// if not, don't do anything new but scale
// if it is, then we can assume between will exist below
// so we don't need the 'bank' function, whatever that is.
// TODO: is this right?
else if(position(projection, mouse1)) {
// go back to original projection temporarily
// except for scale... that's kind of independent?
projection
.rotate(rotate0)
.translate(translate0);
// calculate the new params
var point1 = position(projection, mouse1),
between = rotateBetween(zoomPoint, point1),
newEuler = eulerFromQuaternion(multiply(q, between)),
rotateAngles = view.r = unRoll(newEuler, zoomPoint, lastRotate);
if(!isFinite(rotateAngles[0]) || !isFinite(rotateAngles[1]) ||
!isFinite(rotateAngles[2])) {
rotateAngles = lastRotate;
}
// update the projection
projection.rotate(rotateAngles);
lastRotate = rotateAngles;
}
zoomed(event.of(this, arguments));
});
zoomstarted(event.of(this, arguments));
})
.on('zoomend', function() {
d3.select(this).style(zoomendStyle);
zoomOn.call(zoom, 'zoom', null);
zoomended(event.of(this, arguments));
})
.on('zoom.redraw', function() {
geo.render();
});
function zoomstarted(dispatch) {
if(!zooming++) dispatch({type: 'zoomstart'});
}
function zoomed(dispatch) {
dispatch({type: 'zoom'});
}
function zoomended(dispatch) {
if(!--zooming) dispatch({type: 'zoomend'});
}
return d3.rebind(zoom, event, 'on');
}
// -- helper functions for zoomClipped
function position(projection, point) {
var spherical = projection.invert(point);
return spherical && isFinite(spherical[0]) && isFinite(spherical[1]) && cartesian(spherical);
}
function quaternionFromEuler(euler) {
var lambda = 0.5 * euler[0] * radians,
phi = 0.5 * euler[1] * radians,
gamma = 0.5 * euler[2] * radians,
sinLambda = Math.sin(lambda), cosLambda = Math.cos(lambda),
sinPhi = Math.sin(phi), cosPhi = Math.cos(phi),
sinGamma = Math.sin(gamma), cosGamma = Math.cos(gamma);
return [
cosLambda * cosPhi * cosGamma + sinLambda * sinPhi * sinGamma,
sinLambda * cosPhi * cosGamma - cosLambda * sinPhi * sinGamma,
cosLambda * sinPhi * cosGamma + sinLambda * cosPhi * sinGamma,
cosLambda * cosPhi * sinGamma - sinLambda * sinPhi * cosGamma
];
}
function multiply(a, b) {
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3],
b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3];
return [
a0 * b0 - a1 * b1 - a2 * b2 - a3 * b3,
a0 * b1 + a1 * b0 + a2 * b3 - a3 * b2,
a0 * b2 - a1 * b3 + a2 * b0 + a3 * b1,
a0 * b3 + a1 * b2 - a2 * b1 + a3 * b0
];
}
function rotateBetween(a, b) {
if(!a || !b) return;
var axis = cross(a, b),
norm = Math.sqrt(dot(axis, axis)),
halfgamma = 0.5 * Math.acos(Math.max(-1, Math.min(1, dot(a, b)))),
k = Math.sin(halfgamma) / norm;
return norm && [Math.cos(halfgamma), axis[2] * k, -axis[1] * k, axis[0] * k];
}
// input:
// rotateAngles: a calculated set of Euler angles
// pt: a point (cartesian in 3-space) to keep fixed
// roll0: an initial roll, to be preserved
// output:
// a set of Euler angles that preserve the projection of pt
// but set roll (output[2]) equal to roll0
// note that this doesn't depend on the particular projection,
// just on the rotation angles
function unRoll(rotateAngles, pt, lastRotate) {
// calculate the fixed point transformed by these Euler angles
// but with the desired roll undone
var ptRotated = rotateCartesian(pt, 2, rotateAngles[0]);
ptRotated = rotateCartesian(ptRotated, 1, rotateAngles[1]);
ptRotated = rotateCartesian(ptRotated, 0, rotateAngles[2] - lastRotate[2]);
var x = pt[0],
y = pt[1],
z = pt[2],
f = ptRotated[0],
g = ptRotated[1],
h = ptRotated[2],
// the following essentially solves:
// ptRotated = rotateCartesian(rotateCartesian(pt, 2, newYaw), 1, newPitch)
// for newYaw and newPitch, as best it can
theta = Math.atan2(y, x) * degrees,
a = Math.sqrt(x * x + y * y),
b,
newYaw1;
if(Math.abs(g) > a) {
newYaw1 = (g > 0 ? 90 : -90) - theta;
b = 0;
} else {
newYaw1 = Math.asin(g / a) * degrees - theta;
b = Math.sqrt(a * a - g * g);
}
var newYaw2 = 180 - newYaw1 - 2 * theta,
newPitch1 = (Math.atan2(h, f) - Math.atan2(z, b)) * degrees,
newPitch2 = (Math.atan2(h, f) - Math.atan2(z, -b)) * degrees;
// which is closest to lastRotate[0,1]: newYaw/Pitch or newYaw2/Pitch2?
var dist1 = angleDistance(lastRotate[0], lastRotate[1], newYaw1, newPitch1),
dist2 = angleDistance(lastRotate[0], lastRotate[1], newYaw2, newPitch2);
if(dist1 <= dist2) return [newYaw1, newPitch1, lastRotate[2]];
else return [newYaw2, newPitch2, lastRotate[2]];
}
function angleDistance(yaw0, pitch0, yaw1, pitch1) {
var dYaw = angleMod(yaw1 - yaw0),
dPitch = angleMod(pitch1 - pitch0);
return Math.sqrt(dYaw * dYaw + dPitch * dPitch);
}
// reduce an angle in degrees to [-180,180]
function angleMod(angle) {
return (angle % 360 + 540) % 360 - 180;
}
// rotate a cartesian vector
// axis is 0 (x), 1 (y), or 2 (z)
// angle is in degrees
function rotateCartesian(vector, axis, angle) {
var angleRads = angle * radians,
vectorOut = vector.slice(),
ax1 = (axis === 0) ? 1 : 0,
ax2 = (axis === 2) ? 1 : 2,
cosa = Math.cos(angleRads),
sina = Math.sin(angleRads);
vectorOut[ax1] = vector[ax1] * cosa - vector[ax2] * sina;
vectorOut[ax2] = vector[ax2] * cosa + vector[ax1] * sina;
return vectorOut;
}
function eulerFromQuaternion(q) {
return [
Math.atan2(2 * (q[0] * q[1] + q[2] * q[3]), 1 - 2 * (q[1] * q[1] + q[2] * q[2])) * degrees,
Math.asin(Math.max(-1, Math.min(1, 2 * (q[0] * q[2] - q[3] * q[1])))) * degrees,
Math.atan2(2 * (q[0] * q[3] + q[1] * q[2]), 1 - 2 * (q[2] * q[2] + q[3] * q[3])) * degrees
];
}
function cartesian(spherical) {
var lambda = spherical[0] * radians,
phi = spherical[1] * radians,
cosPhi = Math.cos(phi);
return [
cosPhi * Math.cos(lambda),
cosPhi * Math.sin(lambda),
Math.sin(phi)
];
}
function dot(a, b) {
var s = 0;
for(var i = 0, n = a.length; i < n; ++i) s += a[i] * b[i];
return s;
}
function cross(a, b) {
return [
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0]
];
}
// Like d3.dispatch, but for custom events abstracting native UI events. These
// events have a target component (such as a brush), a target element (such as
// the svg:g element containing the brush) and the standard arguments `d` (the
// target element's data) and `i` (the selection index of the target element).
function d3_eventDispatch(target) {
var i = 0,
n = arguments.length,
argumentz = [];
while(++i < n) argumentz.push(arguments[i]);
var dispatch = d3.dispatch.apply(null, argumentz);
// Creates a dispatch context for the specified `thiz` (typically, the target
// DOM element that received the source event) and `argumentz` (typically, the
// data `d` and index `i` of the target element). The returned function can be
// used to dispatch an event to any registered listeners; the function takes a
// single argument as input, being the event to dispatch. The event must have
// a "type" attribute which corresponds to a type registered in the
// constructor. This context will automatically populate the "sourceEvent" and
// "target" attributes of the event, as well as setting the `d3.event` global
// for the duration of the notification.
dispatch.of = function(thiz, argumentz) {
return function(e1) {
var e0;
try {
e0 = e1.sourceEvent = d3.event;
e1.target = target;
d3.event = e1;
dispatch[e1.type].apply(thiz, argumentz);
} finally {
d3.event = e0;
}
};
};
return dispatch;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function createGeoZoomReset(geo, geoLayout) { var projection = geo.projection, zoom = geo.zoom; var zoomReset = function() { geo.makeProjection(geoLayout); geo.makePath(); zoom.scale(projection.scale()); zoom.translate(projection.translate()); geo.render(); }; return zoomReset; }; |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| camera.js | 5.92% | (9 / 152) | 0% | (0 / 63) | 0% | (0 / 7) | 6.12% | (9 / 147) | |
| convert.js | 12.03% | (16 / 133) | 0% | (0 / 74) | 0% | (0 / 9) | 13.33% | (16 / 120) | |
| scene2d.js | 1.88% | (6 / 319) | 0% | (0 / 101) | 0% | (0 / 20) | 1.99% | (6 / 302) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var mouseChange = require('mouse-change');
var mouseWheel = require('mouse-wheel');
var cartesianConstants = require('../cartesian/constants');
module.exports = createCamera;
function Camera2D(element, plot) {
this.element = element;
this.plot = plot;
this.mouseListener = null;
this.wheelListener = null;
this.lastInputTime = Date.now();
this.lastPos = [0, 0];
this.boxEnabled = false;
this.boxInited = false;
this.boxStart = [0, 0];
this.boxEnd = [0, 0];
this.dragStart = [0, 0];
}
function createCamera(scene) {
var element = scene.mouseContainer,
plot = scene.glplot,
result = new Camera2D(element, plot);
function unSetAutoRange() {
scene.xaxis.autorange = false;
scene.yaxis.autorange = false;
}
function getSubplotConstraint() {
// note: this assumes we only have one x and one y axis on this subplot
// when this constraint is lifted this block won't make sense
var constraints = scene.graphDiv._fullLayout._axisConstraintGroups;
var xaId = scene.xaxis._id;
var yaId = scene.yaxis._id;
for(var i = 0; i < constraints.length; i++) {
if(constraints[i][xaId] !== -1) {
if(constraints[i][yaId] !== -1) return true;
break;
}
}
return false;
}
result.mouseListener = mouseChange(element, function(buttons, x, y) {
var dataBox = scene.calcDataBox(),
viewBox = plot.viewBox;
var lastX = result.lastPos[0],
lastY = result.lastPos[1];
var MINDRAG = cartesianConstants.MINDRAG * plot.pixelRatio;
var MINZOOM = cartesianConstants.MINZOOM * plot.pixelRatio;
var dx, dy;
x *= plot.pixelRatio;
y *= plot.pixelRatio;
// mouseChange gives y about top; convert to about bottom
y = (viewBox[3] - viewBox[1]) - y;
function updateRange(i0, start, end) {
var range0 = Math.min(start, end),
range1 = Math.max(start, end);
if(range0 !== range1) {
dataBox[i0] = range0;
dataBox[i0 + 2] = range1;
result.dataBox = dataBox;
scene.setRanges(dataBox);
}
else {
scene.selectBox.selectBox = [0, 0, 1, 1];
scene.glplot.setDirty();
}
}
switch(scene.fullLayout.dragmode) {
case 'zoom':
if(buttons) {
var dataX = x /
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
dataBox[0];
var dataY = y /
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
dataBox[1];
if(!result.boxInited) {
result.boxStart[0] = dataX;
result.boxStart[1] = dataY;
result.dragStart[0] = x;
result.dragStart[1] = y;
}
result.boxEnd[0] = dataX;
result.boxEnd[1] = dataY;
// we need to mark the box as initialized right away
// so that we can tell the start and end pionts apart
result.boxInited = true;
// but don't actually enable the box until the cursor moves
if(!result.boxEnabled && (
result.boxStart[0] !== result.boxEnd[0] ||
result.boxStart[1] !== result.boxEnd[1])
) {
result.boxEnabled = true;
}
// constrain aspect ratio if the axes require it
var smallDx = Math.abs(result.dragStart[0] - x) < MINZOOM;
var smallDy = Math.abs(result.dragStart[1] - y) < MINZOOM;
if(getSubplotConstraint() && !(smallDx && smallDy)) {
dx = result.boxEnd[0] - result.boxStart[0];
dy = result.boxEnd[1] - result.boxStart[1];
var dydx = (dataBox[3] - dataBox[1]) / (dataBox[2] - dataBox[0]);
if(Math.abs(dx * dydx) > Math.abs(dy)) {
result.boxEnd[1] = result.boxStart[1] +
Math.abs(dx) * dydx * (Math.sign(dy) || 1);
// gl-select-box clips to the plot area bounds,
// which breaks the axis constraint, so don't allow
// this box to go out of bounds
if(result.boxEnd[1] < dataBox[1]) {
result.boxEnd[1] = dataBox[1];
result.boxEnd[0] = result.boxStart[0] +
(dataBox[1] - result.boxStart[1]) / Math.abs(dydx);
}
else if(result.boxEnd[1] > dataBox[3]) {
result.boxEnd[1] = dataBox[3];
result.boxEnd[0] = result.boxStart[0] +
(dataBox[3] - result.boxStart[1]) / Math.abs(dydx);
}
}
else {
result.boxEnd[0] = result.boxStart[0] +
Math.abs(dy) / dydx * (Math.sign(dx) || 1);
if(result.boxEnd[0] < dataBox[0]) {
result.boxEnd[0] = dataBox[0];
result.boxEnd[1] = result.boxStart[1] +
(dataBox[0] - result.boxStart[0]) * Math.abs(dydx);
}
else if(result.boxEnd[0] > dataBox[2]) {
result.boxEnd[0] = dataBox[2];
result.boxEnd[1] = result.boxStart[1] +
(dataBox[2] - result.boxStart[0]) * Math.abs(dydx);
}
}
}
// otherwise clamp small changes to the origin so we get 1D zoom
else {
if(smallDx) result.boxEnd[0] = result.boxStart[0];
if(smallDy) result.boxEnd[1] = result.boxStart[1];
}
}
else if(result.boxEnabled) {
dx = result.boxStart[0] !== result.boxEnd[0];
dy = result.boxStart[1] !== result.boxEnd[1];
if(dx || dy) {
if(dx) {
updateRange(0, result.boxStart[0], result.boxEnd[0]);
scene.xaxis.autorange = false;
}
if(dy) {
updateRange(1, result.boxStart[1], result.boxEnd[1]);
scene.yaxis.autorange = false;
}
scene.relayoutCallback();
}
else {
scene.glplot.setDirty();
}
result.boxEnabled = false;
result.boxInited = false;
}
break;
case 'pan':
result.boxEnabled = false;
result.boxInited = false;
if(buttons) {
if(!result.panning) {
result.dragStart[0] = x;
result.dragStart[1] = y;
}
if(Math.abs(result.dragStart[0] - x) < MINDRAG) x = result.dragStart[0];
if(Math.abs(result.dragStart[1] - y) < MINDRAG) y = result.dragStart[1];
dx = (lastX - x) * (dataBox[2] - dataBox[0]) /
(plot.viewBox[2] - plot.viewBox[0]);
dy = (lastY - y) * (dataBox[3] - dataBox[1]) /
(plot.viewBox[3] - plot.viewBox[1]);
dataBox[0] += dx;
dataBox[2] += dx;
dataBox[1] += dy;
dataBox[3] += dy;
scene.setRanges(dataBox);
result.panning = true;
result.lastInputTime = Date.now();
unSetAutoRange();
scene.cameraChanged();
scene.handleAnnotations();
}
else if(result.panning) {
result.panning = false;
scene.relayoutCallback();
}
break;
}
result.lastPos[0] = x;
result.lastPos[1] = y;
});
result.wheelListener = mouseWheel(element, function(dx, dy) {
var dataBox = scene.calcDataBox(),
viewBox = plot.viewBox;
var lastX = result.lastPos[0],
lastY = result.lastPos[1];
switch(scene.fullLayout.dragmode) {
case 'zoom':
break;
case 'pan':
var scale = Math.exp(0.1 * dy / (viewBox[3] - viewBox[1]));
var cx = lastX /
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
dataBox[0];
var cy = lastY /
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
dataBox[1];
dataBox[0] = (dataBox[0] - cx) * scale + cx;
dataBox[2] = (dataBox[2] - cx) * scale + cx;
dataBox[1] = (dataBox[1] - cy) * scale + cy;
dataBox[3] = (dataBox[3] - cy) * scale + cy;
scene.setRanges(dataBox);
result.lastInputTime = Date.now();
unSetAutoRange();
scene.cameraChanged();
scene.handleAnnotations();
scene.relayoutCallback();
break;
}
return true;
});
return result;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plots = require('../plots');
var Axes = require('../cartesian/axes');
var convertHTMLToUnicode = require('../../lib/html2unicode');
var str2RGBArray = require('../../lib/str2rgbarray');
function Axes2DOptions(scene) {
this.scene = scene;
this.gl = scene.gl;
this.pixelRatio = scene.pixelRatio;
this.screenBox = [0, 0, 1, 1];
this.viewBox = [0, 0, 1, 1];
this.dataBox = [-1, -1, 1, 1];
this.borderLineEnable = [false, false, false, false];
this.borderLineWidth = [1, 1, 1, 1];
this.borderLineColor = [
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]
];
this.ticks = [[], []];
this.tickEnable = [true, true, false, false];
this.tickPad = [15, 15, 15, 15];
this.tickAngle = [0, 0, 0, 0];
this.tickColor = [
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]
];
this.tickMarkLength = [0, 0, 0, 0];
this.tickMarkWidth = [0, 0, 0, 0];
this.tickMarkColor = [
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]
];
this.labels = ['x', 'y'];
this.labelEnable = [true, true, false, false];
this.labelAngle = [0, Math.PI / 2, 0, 3.0 * Math.PI / 2];
this.labelPad = [15, 15, 15, 15];
this.labelSize = [12, 12];
this.labelFont = ['sans-serif', 'sans-serif'];
this.labelColor = [
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1],
[0, 0, 0, 1]
];
this.title = '';
this.titleEnable = true;
this.titleCenter = [0, 0, 0, 0];
this.titleAngle = 0;
this.titleColor = [0, 0, 0, 1];
this.titleFont = 'sans-serif';
this.titleSize = 18;
this.gridLineEnable = [true, true];
this.gridLineColor = [
[0, 0, 0, 0.5],
[0, 0, 0, 0.5]
];
this.gridLineWidth = [1, 1];
this.zeroLineEnable = [true, true];
this.zeroLineWidth = [1, 1];
this.zeroLineColor = [
[0, 0, 0, 1],
[0, 0, 0, 1]
];
this.borderColor = [0, 0, 0, 0];
this.backgroundColor = [0, 0, 0, 0];
this.static = this.scene.staticPlot;
}
var proto = Axes2DOptions.prototype;
var AXES = ['xaxis', 'yaxis'];
proto.merge = function(options) {
// titles are rendered in SVG
this.titleEnable = false;
this.backgroundColor = str2RGBArray(options.plot_bgcolor);
var axisName, ax, axTitle, axMirror;
var hasAxisInDfltPos, hasAxisInAltrPos, hasSharedAxis, mirrorLines, mirrorTicks;
var i, j;
for(i = 0; i < 2; ++i) {
axisName = AXES[i];
// get options relevant to this subplot,
// '_name' is e.g. xaxis, xaxis2, yaxis, yaxis4 ...
ax = options[this.scene[axisName]._name];
axTitle = /Click to enter .+ title/.test(ax.title) ? '' : ax.title;
for(j = 0; j <= 2; j += 2) {
this.labelEnable[i + j] = false;
this.labels[i + j] = convertHTMLToUnicode(axTitle);
this.labelColor[i + j] = str2RGBArray(ax.titlefont.color);
this.labelFont[i + j] = ax.titlefont.family;
this.labelSize[i + j] = ax.titlefont.size;
this.labelPad[i + j] = this.getLabelPad(axisName, ax);
this.tickEnable[i + j] = false;
this.tickColor[i + j] = str2RGBArray((ax.tickfont || {}).color);
this.tickAngle[i + j] = (ax.tickangle === 'auto') ?
0 :
Math.PI * -ax.tickangle / 180;
this.tickPad[i + j] = this.getTickPad(ax);
this.tickMarkLength[i + j] = 0;
this.tickMarkWidth[i + j] = ax.tickwidth || 0;
this.tickMarkColor[i + j] = str2RGBArray(ax.tickcolor);
this.borderLineEnable[i + j] = false;
this.borderLineColor[i + j] = str2RGBArray(ax.linecolor);
this.borderLineWidth[i + j] = ax.linewidth || 0;
}
hasSharedAxis = this.hasSharedAxis(ax);
hasAxisInDfltPos = this.hasAxisInDfltPos(axisName, ax) && !hasSharedAxis;
hasAxisInAltrPos = this.hasAxisInAltrPos(axisName, ax) && !hasSharedAxis;
axMirror = ax.mirror || false;
mirrorLines = hasSharedAxis ?
(String(axMirror).indexOf('all') !== -1) : // 'all' or 'allticks'
!!axMirror; // all but false
mirrorTicks = hasSharedAxis ?
(axMirror === 'allticks') :
(String(axMirror).indexOf('ticks') !== -1); // 'ticks' or 'allticks'
// Axis titles and tick labels can only appear of one side of the scene
// and are never show on subplots that share existing axes.
if(hasAxisInDfltPos) this.labelEnable[i] = true;
else if(hasAxisInAltrPos) this.labelEnable[i + 2] = true;
if(hasAxisInDfltPos) this.tickEnable[i] = ax.showticklabels;
else if(hasAxisInAltrPos) this.tickEnable[i + 2] = ax.showticklabels;
// Grid lines and ticks can appear on both sides of the scene
// and can appear on subplot that share existing axes via `ax.mirror`.
if(hasAxisInDfltPos || mirrorLines) this.borderLineEnable[i] = ax.showline;
if(hasAxisInAltrPos || mirrorLines) this.borderLineEnable[i + 2] = ax.showline;
if(hasAxisInDfltPos || mirrorTicks) this.tickMarkLength[i] = this.getTickMarkLength(ax);
if(hasAxisInAltrPos || mirrorTicks) this.tickMarkLength[i + 2] = this.getTickMarkLength(ax);
this.gridLineEnable[i] = ax.showgrid;
this.gridLineColor[i] = str2RGBArray(ax.gridcolor);
this.gridLineWidth[i] = ax.gridwidth;
this.zeroLineEnable[i] = ax.zeroline;
this.zeroLineColor[i] = str2RGBArray(ax.zerolinecolor);
this.zeroLineWidth[i] = ax.zerolinewidth;
}
};
// is an axis shared with an already-drawn subplot ?
proto.hasSharedAxis = function(ax) {
var scene = this.scene,
subplotIds = Plots.getSubplotIds(scene.fullLayout, 'gl2d'),
list = Axes.findSubplotsWithAxis(subplotIds, ax);
// if index === 0, then the subplot is already drawn as subplots
// are drawn in order.
return (list.indexOf(scene.id) !== 0);
};
// has an axis in default position (i.e. bottom/left) ?
proto.hasAxisInDfltPos = function(axisName, ax) {
var axSide = ax.side;
if(axisName === 'xaxis') return (axSide === 'bottom');
else if(axisName === 'yaxis') return (axSide === 'left');
};
// has an axis in alternate position (i.e. top/right) ?
proto.hasAxisInAltrPos = function(axisName, ax) {
var axSide = ax.side;
if(axisName === 'xaxis') return (axSide === 'top');
else if(axisName === 'yaxis') return (axSide === 'right');
};
proto.getLabelPad = function(axisName, ax) {
var offsetBase = 1.5,
fontSize = ax.titlefont.size,
showticklabels = ax.showticklabels;
if(axisName === 'xaxis') {
return (ax.side === 'top') ?
-10 + fontSize * (offsetBase + (showticklabels ? 1 : 0)) :
-10 + fontSize * (offsetBase + (showticklabels ? 0.5 : 0));
}
else if(axisName === 'yaxis') {
return (ax.side === 'right') ?
10 + fontSize * (offsetBase + (showticklabels ? 1 : 0.5)) :
10 + fontSize * (offsetBase + (showticklabels ? 0.5 : 0));
}
};
proto.getTickPad = function(ax) {
return (ax.ticks === 'outside') ? 10 + ax.ticklen : 15;
};
proto.getTickMarkLength = function(ax) {
if(!ax.ticks) return 0;
var ticklen = ax.ticklen;
return (ax.ticks === 'inside') ? -ticklen : ticklen;
};
function createAxes2D(scene) {
return new Axes2DOptions(scene);
}
module.exports = createAxes2D;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | 2 2 2 2 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var Fx = require('../../plots/cartesian/graph_interact');
var createPlot2D = require('gl-plot2d');
var createSpikes = require('gl-spikes2d');
var createSelectBox = require('gl-select-box');
var getContext = require('webgl-context');
var createOptions = require('./convert');
var createCamera = require('./camera');
var convertHTMLToUnicode = require('../../lib/html2unicode');
var showNoWebGlMsg = require('../../lib/show_no_webgl_msg');
var enforceAxisConstraints = require('../../plots/cartesian/constraints');
var AXES = ['xaxis', 'yaxis'];
var STATIC_CANVAS, STATIC_CONTEXT;
function Scene2D(options, fullLayout) {
this.container = options.container;
this.graphDiv = options.graphDiv;
this.pixelRatio = options.plotGlPixelRatio || window.devicePixelRatio;
this.id = options.id;
this.staticPlot = !!options.staticPlot;
this.fullData = null;
this.updateRefs(fullLayout);
this.makeFramework();
// update options
this.glplotOptions = createOptions(this);
this.glplotOptions.merge(fullLayout);
// create the plot
this.glplot = createPlot2D(this.glplotOptions);
// create camera
this.camera = createCamera(this);
// trace set
this.traces = {};
// create axes spikes
this.spikes = createSpikes(this.glplot);
this.selectBox = createSelectBox(this.glplot, {
innerFill: false,
outerFill: true
});
// last button state
this.lastButtonState = 0;
// last pick result
this.pickResult = null;
this.bounds = [Infinity, Infinity, -Infinity, -Infinity];
// flag to stop render loop
this.stopped = false;
// redraw the plot
this.redraw = this.draw.bind(this);
this.redraw();
}
module.exports = Scene2D;
var proto = Scene2D.prototype;
proto.makeFramework = function() {
// create canvas and gl context
if(this.staticPlot) {
if(!STATIC_CONTEXT) {
STATIC_CANVAS = document.createElement('canvas');
STATIC_CONTEXT = getContext({
canvas: STATIC_CANVAS,
preserveDrawingBuffer: false,
premultipliedAlpha: true,
antialias: true
});
if(!STATIC_CONTEXT) {
throw new Error('Error creating static canvas/context for image server');
}
}
this.canvas = STATIC_CANVAS;
this.gl = STATIC_CONTEXT;
}
else {
var liveCanvas = document.createElement('canvas');
var gl = getContext({
canvas: liveCanvas,
premultipliedAlpha: true
});
if(!gl) showNoWebGlMsg(this);
this.canvas = liveCanvas;
this.gl = gl;
}
// position the canvas
var canvas = this.canvas;
canvas.style.width = '100%';
canvas.style.height = '100%';
canvas.style.position = 'absolute';
canvas.style.top = '0px';
canvas.style.left = '0px';
canvas.style['pointer-events'] = 'none';
this.updateSize(canvas);
// disabling user select on the canvas
// sanitizes double-clicks interactions
// ref: https://github.com/plotly/plotly.js/issues/744
canvas.className += 'user-select-none';
// create SVG container for hover text
var svgContainer = this.svgContainer = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg');
svgContainer.style.position = 'absolute';
svgContainer.style.top = svgContainer.style.left = '0px';
svgContainer.style.width = svgContainer.style.height = '100%';
svgContainer.style['z-index'] = 20;
svgContainer.style['pointer-events'] = 'none';
// create div to catch the mouse event
var mouseContainer = this.mouseContainer = document.createElement('div');
mouseContainer.style.position = 'absolute';
// append canvas, hover svg and mouse div to container
var container = this.container;
container.appendChild(canvas);
container.appendChild(svgContainer);
container.appendChild(mouseContainer);
};
proto.toImage = function(format) {
if(!format) format = 'png';
this.stopped = true;
if(this.staticPlot) this.container.appendChild(STATIC_CANVAS);
// update canvas size
this.updateSize(this.canvas);
// force redraw
this.glplot.setDirty();
this.glplot.draw();
// grab context and yank out pixels
var gl = this.glplot.gl,
w = gl.drawingBufferWidth,
h = gl.drawingBufferHeight;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
var pixels = new Uint8Array(w * h * 4);
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
// flip pixels
for(var j = 0, k = h - 1; j < k; ++j, --k) {
for(var i = 0; i < w; ++i) {
for(var l = 0; l < 4; ++l) {
var tmp = pixels[4 * (w * j + i) + l];
pixels[4 * (w * j + i) + l] = pixels[4 * (w * k + i) + l];
pixels[4 * (w * k + i) + l] = tmp;
}
}
}
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
var imageData = context.createImageData(w, h);
imageData.data.set(pixels);
context.putImageData(imageData, 0, 0);
var dataURL;
switch(format) {
case 'jpeg':
dataURL = canvas.toDataURL('image/jpeg');
break;
case 'webp':
dataURL = canvas.toDataURL('image/webp');
break;
default:
dataURL = canvas.toDataURL('image/png');
}
if(this.staticPlot) this.container.removeChild(STATIC_CANVAS);
return dataURL;
};
proto.updateSize = function(canvas) {
if(!canvas) canvas = this.canvas;
var pixelRatio = this.pixelRatio,
fullLayout = this.fullLayout;
var width = fullLayout.width,
height = fullLayout.height,
pixelWidth = Math.ceil(pixelRatio * width) |0,
pixelHeight = Math.ceil(pixelRatio * height) |0;
// check for resize
if(canvas.width !== pixelWidth || canvas.height !== pixelHeight) {
canvas.width = pixelWidth;
canvas.height = pixelHeight;
}
// make sure plots render right thing
if(this.redraw) this.redraw();
return canvas;
};
proto.computeTickMarks = function() {
this.xaxis.setScale();
this.yaxis.setScale();
// override _length from backward compatibility
// even though setScale 'should' give the correct result
this.xaxis._length =
this.glplot.viewBox[2] - this.glplot.viewBox[0];
this.yaxis._length =
this.glplot.viewBox[3] - this.glplot.viewBox[1];
var nextTicks = [
Axes.calcTicks(this.xaxis),
Axes.calcTicks(this.yaxis)
];
for(var j = 0; j < 2; ++j) {
for(var i = 0; i < nextTicks[j].length; ++i) {
// coercing tick value (may not be a string) to a string
nextTicks[j][i].text = convertHTMLToUnicode(nextTicks[j][i].text + '');
}
}
return nextTicks;
};
function compareTicks(a, b) {
for(var i = 0; i < 2; ++i) {
var aticks = a[i],
bticks = b[i];
if(aticks.length !== bticks.length) return true;
for(var j = 0; j < aticks.length; ++j) {
if(aticks[j].x !== bticks[j].x) return true;
}
}
return false;
}
proto.updateRefs = function(newFullLayout) {
this.fullLayout = newFullLayout;
var spmatch = Axes.subplotMatch,
xaxisName = 'xaxis' + this.id.match(spmatch)[1],
yaxisName = 'yaxis' + this.id.match(spmatch)[2];
this.xaxis = this.fullLayout[xaxisName];
this.yaxis = this.fullLayout[yaxisName];
};
proto.relayoutCallback = function() {
var graphDiv = this.graphDiv,
xaxis = this.xaxis,
yaxis = this.yaxis,
layout = graphDiv.layout;
// update user layout
layout.xaxis.autorange = xaxis.autorange;
layout.xaxis.range = xaxis.range.slice(0);
layout.yaxis.autorange = yaxis.autorange;
layout.yaxis.range = yaxis.range.slice(0);
// make a meaningful value to be passed on to the possible 'plotly_relayout' subscriber(s)
// scene.camera has no many useful projection or scale information
// helps determine which one is the latest input (if async)
var update = {
lastInputTime: this.camera.lastInputTime
};
update[xaxis._name] = xaxis.range.slice(0);
update[yaxis._name] = yaxis.range.slice(0);
graphDiv.emit('plotly_relayout', update);
};
proto.cameraChanged = function() {
var camera = this.camera;
this.glplot.setDataBox(this.calcDataBox());
var nextTicks = this.computeTickMarks();
var curTicks = this.glplotOptions.ticks;
if(compareTicks(nextTicks, curTicks)) {
this.glplotOptions.ticks = nextTicks;
this.glplotOptions.dataBox = camera.dataBox;
this.glplot.update(this.glplotOptions);
this.handleAnnotations();
}
};
proto.handleAnnotations = function() {
var gd = this.graphDiv,
annotations = this.fullLayout.annotations;
for(var i = 0; i < annotations.length; i++) {
var ann = annotations[i];
if(ann.xref === this.xaxis._id && ann.yref === this.yaxis._id) {
Registry.getComponentMethod('annotations', 'drawOne')(gd, i);
}
}
};
proto.destroy = function() {
var traces = this.traces;
if(traces) {
Object.keys(traces).map(function(key) {
traces[key].dispose();
delete traces[key];
});
}
this.glplot.dispose();
if(!this.staticPlot) this.container.removeChild(this.canvas);
this.container.removeChild(this.svgContainer);
this.container.removeChild(this.mouseContainer);
this.fullData = null;
this.glplot = null;
this.stopped = true;
};
proto.plot = function(fullData, calcData, fullLayout) {
var glplot = this.glplot;
this.updateRefs(fullLayout);
this.updateTraces(fullData, calcData);
var width = fullLayout.width,
height = fullLayout.height;
this.updateSize(this.canvas);
var options = this.glplotOptions;
options.merge(fullLayout);
options.screenBox = [0, 0, width, height];
var size = fullLayout._size,
domainX = this.xaxis.domain,
domainY = this.yaxis.domain;
options.viewBox = [
size.l + domainX[0] * size.w,
size.b + domainY[0] * size.h,
(width - size.r) - (1 - domainX[1]) * size.w,
(height - size.t) - (1 - domainY[1]) * size.h
];
this.mouseContainer.style.width = size.w * (domainX[1] - domainX[0]) + 'px';
this.mouseContainer.style.height = size.h * (domainY[1] - domainY[0]) + 'px';
this.mouseContainer.height = size.h * (domainY[1] - domainY[0]);
this.mouseContainer.style.left = size.l + domainX[0] * size.w + 'px';
this.mouseContainer.style.top = size.t + (1 - domainY[1]) * size.h + 'px';
var bounds = this.bounds;
bounds[0] = bounds[1] = Infinity;
bounds[2] = bounds[3] = -Infinity;
var traceIds = Object.keys(this.traces);
var ax, i;
for(i = 0; i < traceIds.length; ++i) {
var traceObj = this.traces[traceIds[i]];
for(var k = 0; k < 2; ++k) {
bounds[k] = Math.min(bounds[k], traceObj.bounds[k]);
bounds[k + 2] = Math.max(bounds[k + 2], traceObj.bounds[k + 2]);
}
}
for(i = 0; i < 2; ++i) {
if(bounds[i] > bounds[i + 2]) {
bounds[i] = -1;
bounds[i + 2] = 1;
}
ax = this[AXES[i]];
ax._length = options.viewBox[i + 2] - options.viewBox[i];
Axes.doAutoRange(ax);
ax.setScale();
}
var mockLayout = {
_axisConstraintGroups: this.graphDiv._fullLayout._axisConstraintGroups,
xaxis: this.xaxis,
yaxis: this.yaxis
};
enforceAxisConstraints({_fullLayout: mockLayout});
options.ticks = this.computeTickMarks();
options.dataBox = this.calcDataBox();
options.merge(fullLayout);
glplot.update(options);
// force redraw so that promise is returned when rendering is completed
this.glplot.draw();
};
proto.calcDataBox = function() {
var xaxis = this.xaxis,
yaxis = this.yaxis,
xrange = xaxis.range,
yrange = yaxis.range,
xr2l = xaxis.r2l,
yr2l = yaxis.r2l;
return [xr2l(xrange[0]), yr2l(yrange[0]), xr2l(xrange[1]), yr2l(yrange[1])];
};
proto.setRanges = function(dataBox) {
var xaxis = this.xaxis,
yaxis = this.yaxis,
xl2r = xaxis.l2r,
yl2r = yaxis.l2r;
xaxis.range = [xl2r(dataBox[0]), xl2r(dataBox[2])];
yaxis.range = [yl2r(dataBox[1]), yl2r(dataBox[3])];
};
proto.updateTraces = function(fullData, calcData) {
var traceIds = Object.keys(this.traces);
var i, j, fullTrace;
this.fullData = fullData;
// remove empty traces
trace_id_loop:
for(i = 0; i < traceIds.length; i++) {
var oldUid = traceIds[i],
oldTrace = this.traces[oldUid];
for(j = 0; j < fullData.length; j++) {
fullTrace = fullData[j];
if(fullTrace.uid === oldUid && fullTrace.type === oldTrace.type) {
continue trace_id_loop;
}
}
oldTrace.dispose();
delete this.traces[oldUid];
}
// update / create trace objects
for(i = 0; i < fullData.length; i++) {
fullTrace = fullData[i];
var calcTrace = calcData[i],
traceObj = this.traces[fullTrace.uid];
if(traceObj) traceObj.update(fullTrace, calcTrace);
else {
traceObj = fullTrace._module.plot(this, fullTrace, calcTrace);
this.traces[fullTrace.uid] = traceObj;
}
}
// order object per traces
this.glplot.objects.sort(function(a, b) {
return a._trace.index - b._trace.index;
});
};
proto.emitPointAction = function(nextSelection, eventType) {
var uid = nextSelection.trace.uid;
var trace;
for(var i = 0; i < this.fullData.length; i++) {
if(this.fullData[i].uid === uid) {
trace = this.fullData[i];
}
}
this.graphDiv.emit(eventType, {
points: [{
x: nextSelection.traceCoord[0],
y: nextSelection.traceCoord[1],
curveNumber: trace.index,
pointNumber: nextSelection.pointIndex,
data: trace._input,
fullData: this.fullData,
xaxis: this.xaxis,
yaxis: this.yaxis
}]
});
};
proto.draw = function() {
if(this.stopped) return;
requestAnimationFrame(this.redraw);
var glplot = this.glplot,
camera = this.camera,
mouseListener = camera.mouseListener,
mouseUp = this.lastButtonState === 1 && mouseListener.buttons === 0,
fullLayout = this.fullLayout;
this.lastButtonState = mouseListener.buttons;
this.cameraChanged();
var x = mouseListener.x * glplot.pixelRatio;
var y = this.canvas.height - glplot.pixelRatio * mouseListener.y;
var result;
if(camera.boxEnabled && fullLayout.dragmode === 'zoom') {
this.selectBox.enabled = true;
var selectBox = this.selectBox.selectBox = [
Math.min(camera.boxStart[0], camera.boxEnd[0]),
Math.min(camera.boxStart[1], camera.boxEnd[1]),
Math.max(camera.boxStart[0], camera.boxEnd[0]),
Math.max(camera.boxStart[1], camera.boxEnd[1])
];
// 1D zoom
for(var i = 0; i < 2; i++) {
if(camera.boxStart[i] === camera.boxEnd[i]) {
selectBox[i] = glplot.dataBox[i];
selectBox[i + 2] = glplot.dataBox[i + 2];
}
}
glplot.setDirty();
}
else if(!camera.panning) {
this.selectBox.enabled = false;
var size = fullLayout._size,
domainX = this.xaxis.domain,
domainY = this.yaxis.domain;
result = glplot.pick(
(x / glplot.pixelRatio) + size.l + domainX[0] * size.w,
(y / glplot.pixelRatio) - (size.t + (1 - domainY[1]) * size.h)
);
var nextSelection = result && result.object._trace.handlePick(result);
if(nextSelection && mouseUp) {
this.emitPointAction(nextSelection, 'plotly_click');
}
if(result && result.object._trace.hoverinfo !== 'skip' && fullLayout.hovermode) {
if(nextSelection && (
!this.lastPickResult ||
this.lastPickResult.traceUid !== nextSelection.trace.uid ||
this.lastPickResult.dataCoord[0] !== nextSelection.dataCoord[0] ||
this.lastPickResult.dataCoord[1] !== nextSelection.dataCoord[1])
) {
var selection = nextSelection;
this.lastPickResult = {
traceUid: nextSelection.trace ? nextSelection.trace.uid : null,
dataCoord: nextSelection.dataCoord.slice()
};
this.spikes.update({ center: result.dataCoord });
selection.screenCoord = [
((glplot.viewBox[2] - glplot.viewBox[0]) *
(result.dataCoord[0] - glplot.dataBox[0]) /
(glplot.dataBox[2] - glplot.dataBox[0]) + glplot.viewBox[0]) /
glplot.pixelRatio,
(this.canvas.height - (glplot.viewBox[3] - glplot.viewBox[1]) *
(result.dataCoord[1] - glplot.dataBox[1]) /
(glplot.dataBox[3] - glplot.dataBox[1]) - glplot.viewBox[1]) /
glplot.pixelRatio
];
// this needs to happen before the next block that deletes traceCoord data
// also it's important to copy, otherwise data is lost by the time event data is read
this.emitPointAction(nextSelection, 'plotly_hover');
var hoverinfo = selection.hoverinfo;
if(hoverinfo !== 'all') {
var parts = hoverinfo.split('+');
if(parts.indexOf('x') === -1) selection.traceCoord[0] = undefined;
if(parts.indexOf('y') === -1) selection.traceCoord[1] = undefined;
if(parts.indexOf('z') === -1) selection.traceCoord[2] = undefined;
if(parts.indexOf('text') === -1) selection.textLabel = undefined;
if(parts.indexOf('name') === -1) selection.name = undefined;
}
Fx.loneHover({
x: selection.screenCoord[0],
y: selection.screenCoord[1],
xLabel: this.hoverFormatter('xaxis', selection.traceCoord[0]),
yLabel: this.hoverFormatter('yaxis', selection.traceCoord[1]),
zLabel: selection.traceCoord[2],
text: selection.textLabel,
name: selection.name,
color: selection.color
}, {
container: this.svgContainer
});
}
}
}
// Remove hover effects if we're not over a point OR
// if we're zooming or panning (in which case result is not set)
if(!result && this.lastPickResult) {
this.spikes.update({});
this.lastPickResult = null;
this.graphDiv.emit('plotly_unhover');
Fx.loneUnhover(this.svgContainer);
}
glplot.draw();
};
proto.hoverFormatter = function(axisName, val) {
if(val === undefined) return undefined;
var axis = this[axisName];
return Axes.tickText(axis, axis.c2l(val), 'hover').text;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 2.33% | (1 / 43) | 0% | (0 / 12) | 0% | (0 / 4) | 2.44% | (1 / 41) | |
| project.js | 27.27% | (3 / 11) | 100% | (0 / 0) | 0% | (0 / 2) | 27.27% | (3 / 11) | |
| scene.js | 2.98% | (11 / 369) | 0% | (0 / 133) | 0% | (0 / 23) | 3.13% | (11 / 351) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | 4 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Scene = require('./scene');
var Plots = require('../plots');
var Lib = require('../../lib');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
exports.name = 'gl3d';
exports.attr = 'scene';
exports.idRoot = 'scene';
exports.idRegex = /^scene([2-9]|[1-9][0-9]+)?$/;
exports.attrRegex = /^scene([2-9]|[1-9][0-9]+)?$/;
exports.attributes = require('./layout/attributes');
exports.layoutAttributes = require('./layout/layout_attributes');
exports.supplyLayoutDefaults = require('./layout/defaults');
exports.plot = function plotGl3d(gd) {
var fullLayout = gd._fullLayout,
fullData = gd._fullData,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');
for(var i = 0; i < sceneIds.length; i++) {
var sceneId = sceneIds[i],
fullSceneData = Plots.getSubplotData(fullData, 'gl3d', sceneId),
sceneLayout = fullLayout[sceneId],
scene = sceneLayout._scene;
if(!scene) {
scene = new Scene({
id: sceneId,
graphDiv: gd,
container: gd.querySelector('.gl-container'),
staticPlot: gd._context.staticPlot,
plotGlPixelRatio: gd._context.plotGlPixelRatio
},
fullLayout
);
// set ref to Scene instance
sceneLayout._scene = scene;
}
// save 'initial' camera settings for modebar button
if(!scene.cameraInitial) {
scene.cameraInitial = Lib.extendDeep({}, sceneLayout.camera);
}
scene.plot(fullSceneData, fullLayout, gd.layout);
}
};
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var oldSceneKeys = Plots.getSubplotIds(oldFullLayout, 'gl3d');
for(var i = 0; i < oldSceneKeys.length; i++) {
var oldSceneKey = oldSceneKeys[i];
if(!newFullLayout[oldSceneKey] && !!oldFullLayout[oldSceneKey]._scene) {
oldFullLayout[oldSceneKey]._scene.destroy();
}
}
};
exports.toSVG = function(gd) {
var fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d'),
size = fullLayout._size;
for(var i = 0; i < sceneIds.length; i++) {
var sceneLayout = fullLayout[sceneIds[i]],
domain = sceneLayout.domain,
scene = sceneLayout._scene;
var imageData = scene.toImage('png');
var image = fullLayout._glimages.append('svg:image');
image.attr({
xmlns: xmlnsNamespaces.svg,
'xlink:href': imageData,
x: size.l + size.w * domain.x[0],
y: size.t + size.h * (1 - domain.y[1]),
width: size.w * (domain.x[1] - domain.x[0]),
height: size.h * (domain.y[1] - domain.y[0]),
preserveAspectRatio: 'none'
});
scene.destroy();
}
};
// clean scene ids, 'scene1' -> 'scene'
exports.cleanId = function cleanId(id) {
if(!id.match(/^scene[0-9]*$/)) return;
var sceneNum = id.substr(5);
if(sceneNum === '1') sceneNum = '';
return 'scene' + sceneNum;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; function xformMatrix(m, v) { var out = [0, 0, 0, 0]; var i, j; for(i = 0; i < 4; ++i) { for(j = 0; j < 4; ++j) { out[j] += m[4 * i + j] * v[i]; } } return out; } function project(camera, v) { var p = xformMatrix(camera.projection, xformMatrix(camera.view, xformMatrix(camera.model, [v[0], v[1], v[2], 1]))); return p; } module.exports = project; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 | 6 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createPlot = require('gl-plot3d');
var getContext = require('webgl-context');
var Lib = require('../../lib');
var Axes = require('../../plots/cartesian/axes');
var Fx = require('../../plots/cartesian/graph_interact');
var str2RGBAarray = require('../../lib/str2rgbarray');
var showNoWebGlMsg = require('../../lib/show_no_webgl_msg');
var createCamera = require('./camera');
var project = require('./project');
var createAxesOptions = require('./layout/convert');
var createSpikeOptions = require('./layout/spikes');
var computeTickMarks = require('./layout/tick_marks');
var STATIC_CANVAS, STATIC_CONTEXT;
function render(scene) {
var trace;
// update size of svg container
var svgContainer = scene.svgContainer;
var clientRect = scene.container.getBoundingClientRect();
var width = clientRect.width, height = clientRect.height;
svgContainer.setAttributeNS(null, 'viewBox', '0 0 ' + width + ' ' + height);
svgContainer.setAttributeNS(null, 'width', width);
svgContainer.setAttributeNS(null, 'height', height);
computeTickMarks(scene);
scene.glplot.axes.update(scene.axesOptions);
// check if pick has changed
var keys = Object.keys(scene.traces);
var lastPicked = null;
var selection = scene.glplot.selection;
for(var i = 0; i < keys.length; ++i) {
trace = scene.traces[keys[i]];
if(trace.data.hoverinfo !== 'skip' && trace.handlePick(selection)) {
lastPicked = trace;
}
if(trace.setContourLevels) trace.setContourLevels();
}
function formatter(axisName, val) {
var axis = scene.fullSceneLayout[axisName];
return Axes.tickText(axis, axis.d2l(val), 'hover').text;
}
var oldEventData;
if(lastPicked !== null) {
var pdata = project(scene.glplot.cameraParams, selection.dataCoordinate);
trace = lastPicked.data;
var hoverinfo = trace.hoverinfo;
var xVal = formatter('xaxis', selection.traceCoordinate[0]),
yVal = formatter('yaxis', selection.traceCoordinate[1]),
zVal = formatter('zaxis', selection.traceCoordinate[2]);
if(hoverinfo !== 'all') {
var hoverinfoParts = hoverinfo.split('+');
if(hoverinfoParts.indexOf('x') === -1) xVal = undefined;
if(hoverinfoParts.indexOf('y') === -1) yVal = undefined;
if(hoverinfoParts.indexOf('z') === -1) zVal = undefined;
if(hoverinfoParts.indexOf('text') === -1) selection.textLabel = undefined;
if(hoverinfoParts.indexOf('name') === -1) lastPicked.name = undefined;
}
if(scene.fullSceneLayout.hovermode) {
Fx.loneHover({
x: (0.5 + 0.5 * pdata[0] / pdata[3]) * width,
y: (0.5 - 0.5 * pdata[1] / pdata[3]) * height,
xLabel: xVal,
yLabel: yVal,
zLabel: zVal,
text: selection.textLabel,
name: lastPicked.name,
color: lastPicked.color
}, {
container: svgContainer
});
}
var eventData = {
points: [{
x: xVal,
y: yVal,
z: zVal,
data: trace._input,
fullData: trace,
curveNumber: trace.index,
pointNumber: selection.data.index
}]
};
if(selection.buttons && selection.distance < 5) {
scene.graphDiv.emit('plotly_click', eventData);
}
else {
scene.graphDiv.emit('plotly_hover', eventData);
}
oldEventData = eventData;
}
else {
Fx.loneUnhover(svgContainer);
scene.graphDiv.emit('plotly_unhover', oldEventData);
}
}
function initializeGLPlot(scene, fullLayout, canvas, gl) {
var glplotOptions = {
canvas: canvas,
gl: gl,
container: scene.container,
axes: scene.axesOptions,
spikes: scene.spikeOptions,
pickRadius: 10,
snapToData: true,
autoScale: true,
autoBounds: false
};
// for static plots, we reuse the WebGL context
// as WebKit doesn't collect them reliably
if(scene.staticMode) {
if(!STATIC_CONTEXT) {
STATIC_CANVAS = document.createElement('canvas');
STATIC_CONTEXT = getContext({
canvas: STATIC_CANVAS,
preserveDrawingBuffer: true,
premultipliedAlpha: true,
antialias: true
});
if(!STATIC_CONTEXT) {
throw new Error('error creating static canvas/context for image server');
}
}
glplotOptions.pixelRatio = scene.pixelRatio;
glplotOptions.gl = STATIC_CONTEXT;
glplotOptions.canvas = STATIC_CANVAS;
}
try {
scene.glplot = createPlot(glplotOptions);
}
catch(e) {
/*
* createPlot will throw when webgl is not enabled in the client.
* Lets return an instance of the module with all functions noop'd.
* The destroy method - which will remove the container from the DOM
* is overridden with a function that removes the container only.
*/
showNoWebGlMsg(scene);
}
var relayoutCallback = function(scene) {
if(scene.fullSceneLayout.dragmode === false) return;
var update = {};
update[scene.id] = getLayoutCamera(scene.camera);
scene.saveCamera(scene.graphDiv.layout);
scene.graphDiv.emit('plotly_relayout', update);
};
scene.glplot.canvas.addEventListener('mouseup', relayoutCallback.bind(null, scene));
scene.glplot.canvas.addEventListener('wheel', relayoutCallback.bind(null, scene));
if(!scene.staticMode) {
scene.glplot.canvas.addEventListener('webglcontextlost', function(ev) {
Lib.warn('Lost WebGL context.');
ev.preventDefault();
});
}
if(!scene.camera) {
var cameraData = scene.fullSceneLayout.camera;
scene.camera = createCamera(scene.container, {
center: [cameraData.center.x, cameraData.center.y, cameraData.center.z],
eye: [cameraData.eye.x, cameraData.eye.y, cameraData.eye.z],
up: [cameraData.up.x, cameraData.up.y, cameraData.up.z],
zoomMin: 0.1,
zoomMax: 100,
mode: 'orbit'
});
}
scene.glplot.camera = scene.camera;
scene.glplot.oncontextloss = function() {
scene.recoverContext();
};
scene.glplot.onrender = render.bind(null, scene);
// List of scene objects
scene.traces = {};
return true;
}
function Scene(options, fullLayout) {
// create sub container for plot
var sceneContainer = document.createElement('div');
var plotContainer = options.container;
// keep a ref to the graph div to fire hover+click events
this.graphDiv = options.graphDiv;
// create SVG container for hover text
var svgContainer = document.createElementNS(
'http://www.w3.org/2000/svg',
'svg');
svgContainer.style.position = 'absolute';
svgContainer.style.top = svgContainer.style.left = '0px';
svgContainer.style.width = svgContainer.style.height = '100%';
svgContainer.style['z-index'] = 20;
svgContainer.style['pointer-events'] = 'none';
sceneContainer.appendChild(svgContainer);
this.svgContainer = svgContainer;
// Tag the container with the sceneID
sceneContainer.id = options.id;
sceneContainer.style.position = 'absolute';
sceneContainer.style.top = sceneContainer.style.left = '0px';
sceneContainer.style.width = sceneContainer.style.height = '100%';
plotContainer.appendChild(sceneContainer);
this.fullLayout = fullLayout;
this.id = options.id || 'scene';
this.fullSceneLayout = fullLayout[this.id];
// Saved from last call to plot()
this.plotArgs = [ [], {}, {} ];
/*
* Move this to calc step? Why does it work here?
*/
this.axesOptions = createAxesOptions(fullLayout[this.id]);
this.spikeOptions = createSpikeOptions(fullLayout[this.id]);
this.container = sceneContainer;
this.staticMode = !!options.staticPlot;
this.pixelRatio = options.plotGlPixelRatio || 2;
// Coordinate rescaling
this.dataScale = [1, 1, 1];
this.contourLevels = [ [], [], [] ];
if(!initializeGLPlot(this, fullLayout)) return; // todo check the necessity for this line
}
var proto = Scene.prototype;
proto.recoverContext = function() {
var scene = this;
var gl = this.glplot.gl;
var canvas = this.glplot.canvas;
this.glplot.dispose();
function tryRecover() {
if(gl.isContextLost()) {
requestAnimationFrame(tryRecover);
return;
}
if(!initializeGLPlot(scene, scene.fullLayout, canvas, gl)) {
Lib.error('Catastrophic and unrecoverable WebGL error. Context lost.');
return;
}
scene.plot.apply(scene, scene.plotArgs);
}
requestAnimationFrame(tryRecover);
};
var axisProperties = [ 'xaxis', 'yaxis', 'zaxis' ];
function coordinateBound(axis, coord, d, bounds, calendar) {
var x;
for(var i = 0; i < coord.length; ++i) {
if(Array.isArray(coord[i])) {
for(var j = 0; j < coord[i].length; ++j) {
x = axis.d2l(coord[i][j], 0, calendar);
if(!isNaN(x) && isFinite(x)) {
bounds[0][d] = Math.min(bounds[0][d], x);
bounds[1][d] = Math.max(bounds[1][d], x);
}
}
}
else {
x = axis.d2l(coord[i], 0, calendar);
if(!isNaN(x) && isFinite(x)) {
bounds[0][d] = Math.min(bounds[0][d], x);
bounds[1][d] = Math.max(bounds[1][d], x);
}
}
}
}
function computeTraceBounds(scene, trace, bounds) {
var sceneLayout = scene.fullSceneLayout;
coordinateBound(sceneLayout.xaxis, trace.x, 0, bounds, trace.xcalendar);
coordinateBound(sceneLayout.yaxis, trace.y, 1, bounds, trace.ycalendar);
coordinateBound(sceneLayout.zaxis, trace.z, 2, bounds, trace.zcalendar);
}
proto.plot = function(sceneData, fullLayout, layout) {
// Save parameters
this.plotArgs = [sceneData, fullLayout, layout];
if(this.glplot.contextLost) return;
var data, trace;
var i, j, axis, axisType;
var fullSceneLayout = fullLayout[this.id];
var sceneLayout = layout[this.id];
if(fullSceneLayout.bgcolor) this.glplot.clearColor = str2RGBAarray(fullSceneLayout.bgcolor);
else this.glplot.clearColor = [0, 0, 0, 0];
this.glplot.snapToData = true;
// Update layout
this.fullLayout = fullLayout;
this.fullSceneLayout = fullSceneLayout;
this.glplotLayout = fullSceneLayout;
this.axesOptions.merge(fullSceneLayout);
this.spikeOptions.merge(fullSceneLayout);
// Update camera and camera mode
this.setCamera(fullSceneLayout.camera);
this.updateFx(fullSceneLayout.dragmode, fullSceneLayout.hovermode);
// Update scene
this.glplot.update({});
// Update axes functions BEFORE updating traces
this.setConvert(axis);
// Convert scene data
if(!sceneData) sceneData = [];
else if(!Array.isArray(sceneData)) sceneData = [sceneData];
// Compute trace bounding box
var dataBounds = [
[Infinity, Infinity, Infinity],
[-Infinity, -Infinity, -Infinity]
];
for(i = 0; i < sceneData.length; ++i) {
data = sceneData[i];
if(data.visible !== true) continue;
computeTraceBounds(this, data, dataBounds);
}
var dataScale = [1, 1, 1];
for(j = 0; j < 3; ++j) {
if(dataBounds[0][j] > dataBounds[1][j]) {
dataScale[j] = 1.0;
}
else {
if(dataBounds[1][j] === dataBounds[0][j]) {
dataScale[j] = 1.0;
}
else {
dataScale[j] = 1.0 / (dataBounds[1][j] - dataBounds[0][j]);
}
}
}
// Save scale
this.dataScale = dataScale;
// Update traces
for(i = 0; i < sceneData.length; ++i) {
data = sceneData[i];
if(data.visible !== true) {
continue;
}
trace = this.traces[data.uid];
if(trace) {
trace.update(data);
} else {
trace = data._module.plot(this, data);
this.traces[data.uid] = trace;
}
trace.name = data.name;
}
// Remove empty traces
var traceIds = Object.keys(this.traces);
trace_id_loop:
for(i = 0; i < traceIds.length; ++i) {
for(j = 0; j < sceneData.length; ++j) {
if(sceneData[j].uid === traceIds[i] && sceneData[j].visible === true) {
continue trace_id_loop;
}
}
trace = this.traces[traceIds[i]];
trace.dispose();
delete this.traces[traceIds[i]];
}
// order object per trace index
this.glplot.objects.sort(function(a, b) {
return a._trace.data.index - b._trace.data.index;
});
// Update ranges (needs to be called *after* objects are added due to updates)
var sceneBounds = [[0, 0, 0], [0, 0, 0]],
axisDataRange = [],
axisTypeRatios = {};
for(i = 0; i < 3; ++i) {
axis = fullSceneLayout[axisProperties[i]];
axisType = axis.type;
if(axisType in axisTypeRatios) {
axisTypeRatios[axisType].acc *= dataScale[i];
axisTypeRatios[axisType].count += 1;
}
else {
axisTypeRatios[axisType] = {
acc: dataScale[i],
count: 1
};
}
if(axis.autorange) {
sceneBounds[0][i] = Infinity;
sceneBounds[1][i] = -Infinity;
for(j = 0; j < this.glplot.objects.length; ++j) {
var objBounds = this.glplot.objects[j].bounds;
sceneBounds[0][i] = Math.min(sceneBounds[0][i],
objBounds[0][i] / dataScale[i]);
sceneBounds[1][i] = Math.max(sceneBounds[1][i],
objBounds[1][i] / dataScale[i]);
}
if('rangemode' in axis && axis.rangemode === 'tozero') {
sceneBounds[0][i] = Math.min(sceneBounds[0][i], 0);
sceneBounds[1][i] = Math.max(sceneBounds[1][i], 0);
}
if(sceneBounds[0][i] > sceneBounds[1][i]) {
sceneBounds[0][i] = -1;
sceneBounds[1][i] = 1;
} else {
var d = sceneBounds[1][i] - sceneBounds[0][i];
sceneBounds[0][i] -= d / 32.0;
sceneBounds[1][i] += d / 32.0;
}
} else {
var range = fullSceneLayout[axisProperties[i]].range;
sceneBounds[0][i] = range[0];
sceneBounds[1][i] = range[1];
}
if(sceneBounds[0][i] === sceneBounds[1][i]) {
sceneBounds[0][i] -= 1;
sceneBounds[1][i] += 1;
}
axisDataRange[i] = sceneBounds[1][i] - sceneBounds[0][i];
// Update plot bounds
this.glplot.bounds[0][i] = sceneBounds[0][i] * dataScale[i];
this.glplot.bounds[1][i] = sceneBounds[1][i] * dataScale[i];
}
var axesScaleRatio = [1, 1, 1];
// Compute axis scale per category
for(i = 0; i < 3; ++i) {
axis = fullSceneLayout[axisProperties[i]];
axisType = axis.type;
var axisRatio = axisTypeRatios[axisType];
axesScaleRatio[i] = Math.pow(axisRatio.acc, 1.0 / axisRatio.count) / dataScale[i];
}
/*
* Dynamically set the aspect ratio depending on the users aspect settings
*/
var axisAutoScaleFactor = 4;
var aspectRatio;
if(fullSceneLayout.aspectmode === 'auto') {
if(Math.max.apply(null, axesScaleRatio) / Math.min.apply(null, axesScaleRatio) <= axisAutoScaleFactor) {
/*
* USE DATA MODE WHEN AXIS RANGE DIMENSIONS ARE RELATIVELY EQUAL
*/
aspectRatio = axesScaleRatio;
} else {
/*
* USE EQUAL MODE WHEN AXIS RANGE DIMENSIONS ARE HIGHLY UNEQUAL
*/
aspectRatio = [1, 1, 1];
}
} else if(fullSceneLayout.aspectmode === 'cube') {
aspectRatio = [1, 1, 1];
} else if(fullSceneLayout.aspectmode === 'data') {
aspectRatio = axesScaleRatio;
} else if(fullSceneLayout.aspectmode === 'manual') {
var userRatio = fullSceneLayout.aspectratio;
aspectRatio = [userRatio.x, userRatio.y, userRatio.z];
} else {
throw new Error('scene.js aspectRatio was not one of the enumerated types');
}
/*
* Write aspect Ratio back to user data and fullLayout so that it is modifies as user
* manipulates the aspectmode settings and the fullLayout is up-to-date.
*/
fullSceneLayout.aspectratio.x = sceneLayout.aspectratio.x = aspectRatio[0];
fullSceneLayout.aspectratio.y = sceneLayout.aspectratio.y = aspectRatio[1];
fullSceneLayout.aspectratio.z = sceneLayout.aspectratio.z = aspectRatio[2];
/*
* Finally assign the computed aspecratio to the glplot module. This will have an effect
* on the next render cycle.
*/
this.glplot.aspect = aspectRatio;
// Update frame position for multi plots
var domain = fullSceneLayout.domain || null,
size = fullLayout._size || null;
if(domain && size) {
var containerStyle = this.container.style;
containerStyle.position = 'absolute';
containerStyle.left = (size.l + domain.x[0] * size.w) + 'px';
containerStyle.top = (size.t + (1 - domain.y[1]) * size.h) + 'px';
containerStyle.width = (size.w * (domain.x[1] - domain.x[0])) + 'px';
containerStyle.height = (size.h * (domain.y[1] - domain.y[0])) + 'px';
}
// force redraw so that promise is returned when rendering is completed
this.glplot.redraw();
};
proto.destroy = function() {
this.glplot.dispose();
this.container.parentNode.removeChild(this.container);
// Remove reference to glplot
this.glplot = null;
};
// getOrbitCamera :: plotly_coords -> orbit_camera_coords
// inverse of getLayoutCamera
function getOrbitCamera(camera) {
return [
[camera.eye.x, camera.eye.y, camera.eye.z],
[camera.center.x, camera.center.y, camera.center.z],
[camera.up.x, camera.up.y, camera.up.z]
];
}
// getLayoutCamera :: orbit_camera_coords -> plotly_coords
// inverse of getOrbitCamera
function getLayoutCamera(camera) {
return {
up: {x: camera.up[0], y: camera.up[1], z: camera.up[2]},
center: {x: camera.center[0], y: camera.center[1], z: camera.center[2]},
eye: {x: camera.eye[0], y: camera.eye[1], z: camera.eye[2]}
};
}
// get camera position in plotly coords from 'orbit-camera' coords
proto.getCamera = function getCamera() {
this.glplot.camera.view.recalcMatrix(this.camera.view.lastT());
return getLayoutCamera(this.glplot.camera);
};
// set camera position with a set of plotly coords
proto.setCamera = function setCamera(cameraData) {
this.glplot.camera.lookAt.apply(this, getOrbitCamera(cameraData));
};
// save camera to user layout (i.e. gd.layout)
proto.saveCamera = function saveCamera(layout) {
var cameraData = this.getCamera(),
cameraNestedProp = Lib.nestedProperty(layout, this.id + '.camera'),
cameraDataLastSave = cameraNestedProp.get(),
hasChanged = false;
function same(x, y, i, j) {
var vectors = ['up', 'center', 'eye'],
components = ['x', 'y', 'z'];
return y[vectors[i]] && (x[vectors[i]][components[j]] === y[vectors[i]][components[j]]);
}
if(cameraDataLastSave === undefined) hasChanged = true;
else {
for(var i = 0; i < 3; i++) {
for(var j = 0; j < 3; j++) {
if(!same(cameraData, cameraDataLastSave, i, j)) {
hasChanged = true;
break;
}
}
}
}
if(hasChanged) cameraNestedProp.set(cameraData);
return hasChanged;
};
proto.updateFx = function(dragmode, hovermode) {
var camera = this.camera;
if(camera) {
// rotate and orbital are synonymous
if(dragmode === 'orbit') {
camera.mode = 'orbit';
camera.keyBindingMode = 'rotate';
} else if(dragmode === 'turntable') {
camera.up = [0, 0, 1];
camera.mode = 'turntable';
camera.keyBindingMode = 'rotate';
} else {
// none rotation modes [pan or zoom]
camera.keyBindingMode = dragmode;
}
}
// to put dragmode and hovermode on the same grounds from relayout
this.fullSceneLayout.hovermode = hovermode;
};
proto.toImage = function(format) {
if(!format) format = 'png';
if(this.staticMode) this.container.appendChild(STATIC_CANVAS);
// Force redraw
this.glplot.redraw();
// Grab context and yank out pixels
var gl = this.glplot.gl;
var w = gl.drawingBufferWidth;
var h = gl.drawingBufferHeight;
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
var pixels = new Uint8Array(w * h * 4);
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
// Flip pixels
for(var j = 0, k = h - 1; j < k; ++j, --k) {
for(var i = 0; i < w; ++i) {
for(var l = 0; l < 4; ++l) {
var tmp = pixels[4 * (w * j + i) + l];
pixels[4 * (w * j + i) + l] = pixels[4 * (w * k + i) + l];
pixels[4 * (w * k + i) + l] = tmp;
}
}
}
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var context = canvas.getContext('2d');
var imageData = context.createImageData(w, h);
imageData.data.set(pixels);
context.putImageData(imageData, 0, 0);
var dataURL;
switch(format) {
case 'jpeg':
dataURL = canvas.toDataURL('image/jpeg');
break;
case 'webp':
dataURL = canvas.toDataURL('image/webp');
break;
default:
dataURL = canvas.toDataURL('image/png');
}
if(this.staticMode) this.container.removeChild(STATIC_CANVAS);
return dataURL;
};
proto.setConvert = function() {
for(var i = 0; i < 3; ++i) {
var ax = this.fullSceneLayout[axisProperties[i]];
Axes.setConvert(ax, this.fullLayout);
ax.setScale = Lib.noop;
}
};
module.exports = Scene;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| convert_text_opts.js | 7.14% | (2 / 28) | 0% | (0 / 14) | 0% | (0 / 1) | 8% | (2 / 25) | |
| layers.js | 17.24% | (15 / 87) | 0% | (0 / 36) | 0% | (0 / 12) | 18.29% | (15 / 82) | |
| mapbox.js | 3.21% | (7 / 218) | 0% | (0 / 30) | 0% | (0 / 37) | 3.3% | (7 / 212) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
/**
* Convert plotly.js 'textposition' to mapbox-gl 'anchor' and 'offset'
* (with the help of the icon size).
*
* @param {string} textpostion : plotly.js textposition value
* @param {number} iconSize : plotly.js icon size (e.g. marker.size for traces)
*
* @return {object}
* - anchor
* - offset
*/
module.exports = function convertTextOpts(textposition, iconSize) {
var parts = textposition.split(' '),
vPos = parts[0],
hPos = parts[1];
// ballpack values
var factor = Array.isArray(iconSize) ? Lib.mean(iconSize) : iconSize,
xInc = 0.5 + (factor / 100),
yInc = 1.5 + (factor / 100);
var anchorVals = ['', ''],
offset = [0, 0];
switch(vPos) {
case 'top':
anchorVals[0] = 'top';
offset[1] = -yInc;
break;
case 'bottom':
anchorVals[0] = 'bottom';
offset[1] = yInc;
break;
}
switch(hPos) {
case 'left':
anchorVals[1] = 'right';
offset[0] = -xInc;
break;
case 'right':
anchorVals[1] = 'left';
offset[0] = xInc;
break;
}
// Mapbox text-anchor must be one of:
// center, left, right, top, bottom,
// top-left, top-right, bottom-left, bottom-right
var anchor;
if(anchorVals[0] && anchorVals[1]) anchor = anchorVals.join('-');
else if(anchorVals[0]) anchor = anchorVals[0];
else if(anchorVals[1]) anchor = anchorVals[1];
else anchor = 'center';
return { anchor: anchor, offset: offset };
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var convertTextOpts = require('./convert_text_opts');
function MapboxLayer(mapbox, index) {
this.mapbox = mapbox;
this.map = mapbox.map;
this.uid = mapbox.uid + '-' + 'layer' + index;
this.idSource = this.uid + '-source';
this.idLayer = this.uid + '-layer';
// some state variable to check if a remove/add step is needed
this.sourceType = null;
this.source = null;
this.layerType = null;
this.below = null;
// is layer currently visible
this.visible = false;
}
var proto = MapboxLayer.prototype;
proto.update = function update(opts) {
if(!this.visible) {
// IMPORTANT: must create source before layer to not cause errors
this.updateSource(opts);
this.updateLayer(opts);
}
else if(this.needsNewSource(opts)) {
// IMPORTANT: must delete layer before source to not cause errors
this.updateLayer(opts);
this.updateSource(opts);
}
else if(this.needsNewLayer(opts)) {
this.updateLayer(opts);
}
this.updateStyle(opts);
this.visible = isVisible(opts);
};
proto.needsNewSource = function(opts) {
// for some reason changing layer to 'fill' or 'symbol'
// w/o changing the source throws an exception in mapbox-gl 0.18 ;
// stay safe and make new source on type changes
return (
this.sourceType !== opts.sourcetype ||
this.source !== opts.source ||
this.layerType !== opts.type
);
};
proto.needsNewLayer = function(opts) {
return (
this.layerType !== opts.type ||
this.below !== opts.below
);
};
proto.updateSource = function(opts) {
var map = this.map;
if(map.getSource(this.idSource)) map.removeSource(this.idSource);
this.sourceType = opts.sourcetype;
this.source = opts.source;
if(!isVisible(opts)) return;
var sourceOpts = convertSourceOpts(opts);
map.addSource(this.idSource, sourceOpts);
};
proto.updateLayer = function(opts) {
var map = this.map;
if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);
this.layerType = opts.type;
if(!isVisible(opts)) return;
map.addLayer({
id: this.idLayer,
source: this.idSource,
'source-layer': opts.sourcelayer || '',
type: opts.type
}, opts.below);
// the only way to make a layer invisible is to remove it
var layoutOpts = { visibility: 'visible' };
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', layoutOpts);
};
proto.updateStyle = function(opts) {
var convertedOpts = convertOpts(opts);
if(isVisible(opts)) {
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', convertedOpts.layout);
this.mapbox.setOptions(this.idLayer, 'setPaintProperty', convertedOpts.paint);
}
};
proto.dispose = function dispose() {
var map = this.map;
map.removeLayer(this.idLayer);
map.removeSource(this.idSource);
};
function isVisible(opts) {
var source = opts.source;
return (
Lib.isPlainObject(source) ||
(typeof source === 'string' && source.length > 0)
);
}
function convertOpts(opts) {
var layout = {},
paint = {};
switch(opts.type) {
case 'circle':
Lib.extendFlat(paint, {
'circle-radius': opts.circle.radius,
'circle-color': opts.color,
'circle-opacity': opts.opacity
});
break;
case 'line':
Lib.extendFlat(paint, {
'line-width': opts.line.width,
'line-color': opts.color,
'line-opacity': opts.opacity
});
break;
case 'fill':
Lib.extendFlat(paint, {
'fill-color': opts.color,
'fill-outline-color': opts.fill.outlinecolor,
'fill-opacity': opts.opacity
// no way to pass specify outline width at the moment
});
break;
case 'symbol':
var symbol = opts.symbol,
textOpts = convertTextOpts(symbol.textposition, symbol.iconsize);
Lib.extendFlat(layout, {
'icon-image': symbol.icon + '-15',
'icon-size': symbol.iconsize / 10,
'text-field': symbol.text,
'text-size': symbol.textfont.size,
'text-anchor': textOpts.anchor,
'text-offset': textOpts.offset
// TODO font family
// 'text-font': symbol.textfont.family.split(', '),
});
Lib.extendFlat(paint, {
'icon-color': opts.color,
'text-color': symbol.textfont.color,
'text-opacity': opts.opacity
});
break;
}
return { layout: layout, paint: paint };
}
function convertSourceOpts(opts) {
var sourceType = opts.sourcetype,
source = opts.source,
sourceOpts = { type: sourceType },
isSourceAString = (typeof source === 'string'),
field;
if(sourceType === 'geojson') field = 'data';
else if(sourceType === 'vector') {
field = isSourceAString ? 'url' : 'tiles';
}
sourceOpts[field] = source;
return sourceOpts;
}
module.exports = function createMapboxLayer(mapbox, index, opts) {
var mapboxLayer = new MapboxLayer(mapbox, index);
mapboxLayer.update(opts);
return mapboxLayer;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 | 2 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var mapboxgl = require('mapbox-gl');
var Fx = require('../cartesian/graph_interact');
var Lib = require('../../lib');
var constants = require('./constants');
var layoutAttributes = require('./layout_attributes');
var createMapboxLayer = require('./layers');
function Mapbox(opts) {
this.id = opts.id;
this.gd = opts.gd;
this.container = opts.container;
this.isStatic = opts.staticPlot;
var fullLayout = opts.fullLayout;
// unique id for this Mapbox instance
this.uid = fullLayout._uid + '-' + this.id;
// full mapbox options (N.B. needs to be updated on every updates)
this.opts = fullLayout[this.id];
// create framework on instantiation for a smoother first plot call
this.div = null;
this.xaxis = null;
this.yaxis = null;
this.createFramework(fullLayout);
// state variables used to infer how and what to update
this.map = null;
this.accessToken = null;
this.styleObj = null;
this.traceHash = {};
this.layerList = [];
}
var proto = Mapbox.prototype;
module.exports = function createMapbox(opts) {
var mapbox = new Mapbox(opts);
return mapbox;
};
proto.plot = function(calcData, fullLayout, promises) {
var self = this;
// feed in new mapbox options
var opts = self.opts = fullLayout[this.id];
// remove map and create a new map if access token has change
if(self.map && (opts.accesstoken !== self.accessToken)) {
self.map.remove();
self.map = null;
self.styleObj = null;
self.traceHash = [];
self.layerList = {};
}
var promise;
if(!self.map) {
promise = new Promise(function(resolve, reject) {
self.createMap(calcData, fullLayout, resolve, reject);
});
}
else {
promise = new Promise(function(resolve, reject) {
self.updateMap(calcData, fullLayout, resolve, reject);
});
}
promises.push(promise);
};
proto.createMap = function(calcData, fullLayout, resolve, reject) {
var self = this,
gd = self.gd,
opts = self.opts;
// store style id and URL or object
var styleObj = self.styleObj = getStyleObj(opts.style);
// store access token associated with this map
self.accessToken = opts.accesstoken;
// create the map!
var map = self.map = new mapboxgl.Map({
container: self.div,
style: styleObj.style,
center: convertCenter(opts.center),
zoom: opts.zoom,
bearing: opts.bearing,
pitch: opts.pitch,
interactive: !self.isStatic,
preserveDrawingBuffer: self.isStatic
});
// clear navigation container
var className = constants.controlContainerClassName;
var controlContainer = self.div.getElementsByClassName(className)[0];
self.div.removeChild(controlContainer);
// make sure canvas does not inherit left and top css
map._canvas.canvas.style.left = '0px';
map._canvas.canvas.style.top = '0px';
self.rejectOnError(reject);
map.once('load', function() {
self.updateData(calcData);
self.updateLayout(fullLayout);
self.resolveOnRender(resolve);
});
// keep track of pan / zoom in user layout and emit relayout event
map.on('moveend', function(eventData) {
if(!self.map) return;
var view = self.getView();
opts._input.center = opts.center = view.center;
opts._input.zoom = opts.zoom = view.zoom;
opts._input.bearing = opts.bearing = view.bearing;
opts._input.pitch = opts.pitch = view.pitch;
// 'moveend' gets triggered by map.setCenter, map.setZoom,
// map.setBearing and map.setPitch.
//
// Here, we make sure that 'plotly_relayout' is
// triggered here only when the 'moveend' originates from a
// mouse target (filtering out API calls) to not
// duplicate 'plotly_relayout' events.
if(eventData.originalEvent) {
var update = {};
update[self.id] = Lib.extendFlat({}, view);
gd.emit('plotly_relayout', update);
}
});
map.on('mousemove', function(evt) {
var bb = self.div.getBoundingClientRect();
// some hackery to get Fx.hover to work
evt.clientX = evt.point.x + bb.left;
evt.clientY = evt.point.y + bb.top;
evt.target.getBoundingClientRect = function() { return bb; };
self.xaxis.p2c = function() { return evt.lngLat.lng; };
self.yaxis.p2c = function() { return evt.lngLat.lat; };
Fx.hover(gd, evt, self.id);
});
map.on('click', function(evt) {
Fx.click(gd, evt.originalEvent);
});
function unhover() {
Fx.loneUnhover(fullLayout._toppaper);
}
map.on('dragstart', unhover);
map.on('zoomstart', unhover);
};
proto.updateMap = function(calcData, fullLayout, resolve, reject) {
var self = this,
map = self.map;
self.rejectOnError(reject);
var styleObj = getStyleObj(self.opts.style);
if(self.styleObj.id !== styleObj.id) {
self.styleObj = styleObj;
map.setStyle(styleObj.style);
map.style.once('load', function() {
// need to rebuild trace layers on reload
// to avoid 'lost event' errors
self.traceHash = {};
self.updateData(calcData);
self.updateLayout(fullLayout);
self.resolveOnRender(resolve);
});
}
else {
self.updateData(calcData);
self.updateLayout(fullLayout);
self.resolveOnRender(resolve);
}
};
proto.updateData = function(calcData) {
var traceHash = this.traceHash;
var traceObj, trace, i, j;
// update or create trace objects
for(i = 0; i < calcData.length; i++) {
var calcTrace = calcData[i];
trace = calcTrace[0].trace;
traceObj = traceHash[trace.uid];
if(traceObj) traceObj.update(calcTrace);
else if(trace._module) {
traceHash[trace.uid] = trace._module.plot(this, calcTrace);
}
}
// remove empty trace objects
var ids = Object.keys(traceHash);
id_loop:
for(i = 0; i < ids.length; i++) {
var id = ids[i];
for(j = 0; j < calcData.length; j++) {
trace = calcData[j][0].trace;
if(id === trace.uid) continue id_loop;
}
traceObj = traceHash[id];
traceObj.dispose();
delete traceHash[id];
}
};
proto.updateLayout = function(fullLayout) {
var map = this.map,
opts = this.opts;
map.setCenter(convertCenter(opts.center));
map.setZoom(opts.zoom);
map.setBearing(opts.bearing);
map.setPitch(opts.pitch);
this.updateLayers();
this.updateFramework(fullLayout);
this.map.resize();
};
proto.resolveOnRender = function(resolve) {
var map = this.map;
map.on('render', function onRender() {
if(map.loaded()) {
map.off('render', onRender);
resolve();
}
});
};
proto.rejectOnError = function(reject) {
var map = this.map;
function handler() {
reject(new Error(constants.mapOnErrorMsg));
}
map.once('error', handler);
map.once('style.error', handler);
map.once('source.error', handler);
map.once('tile.error', handler);
map.once('layer.error', handler);
};
proto.createFramework = function(fullLayout) {
var self = this;
var div = self.div = document.createElement('div');
div.id = self.uid;
div.style.position = 'absolute';
self.container.appendChild(div);
// create mock x/y axes for hover routine
self.xaxis = {
_id: 'x',
c2p: function(v) { return self.project(v).x; }
};
self.yaxis = {
_id: 'y',
c2p: function(v) { return self.project(v).y; }
};
self.updateFramework(fullLayout);
};
proto.updateFramework = function(fullLayout) {
var domain = fullLayout[this.id].domain,
size = fullLayout._size;
var style = this.div.style;
// TODO Is this correct? It seems to get the map zoom level wrong?
style.width = size.w * (domain.x[1] - domain.x[0]) + 'px';
style.height = size.h * (domain.y[1] - domain.y[0]) + 'px';
style.left = size.l + domain.x[0] * size.w + 'px';
style.top = size.t + (1 - domain.y[1]) * size.h + 'px';
this.xaxis._offset = size.l + domain.x[0] * size.w;
this.xaxis._length = size.w * (domain.x[1] - domain.x[0]);
this.yaxis._offset = size.t + (1 - domain.y[1]) * size.h;
this.yaxis._length = size.h * (domain.y[1] - domain.y[0]);
};
proto.updateLayers = function() {
var opts = this.opts,
layers = opts.layers,
layerList = this.layerList,
i;
// if the layer arrays don't match,
// don't try to be smart,
// delete them all, and start all over.
if(layers.length !== layerList.length) {
for(i = 0; i < layerList.length; i++) {
layerList[i].dispose();
}
layerList = this.layerList = [];
for(i = 0; i < layers.length; i++) {
layerList.push(createMapboxLayer(this, i, layers[i]));
}
}
else {
for(i = 0; i < layers.length; i++) {
layerList[i].update(layers[i]);
}
}
};
proto.destroy = function() {
if(this.map) {
this.map.remove();
this.map = null;
}
this.container.removeChild(this.div);
};
proto.toImage = function() {
return this.map.getCanvas().toDataURL();
};
// convenience wrapper to create blank GeoJSON sources
// and avoid 'invalid GeoJSON' errors
proto.initSource = function(idSource) {
var blank = {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: []
}
}
};
return this.map.addSource(idSource, blank);
};
// convenience wrapper to set data of GeoJSON sources
proto.setSourceData = function(idSource, data) {
this.map.getSource(idSource).setData(data);
};
// convenience wrapper to create set multiple layer
// 'layout' or 'paint options at once.
proto.setOptions = function(id, methodName, opts) {
var map = this.map,
keys = Object.keys(opts);
for(var i = 0; i < keys.length; i++) {
var key = keys[i];
map[methodName](id, key, opts[key]);
}
};
// convenience method to project a [lon, lat] array to pixel coords
proto.project = function(v) {
return this.map.project(new mapboxgl.LngLat(v[0], v[1]));
};
// get map's current view values in plotly.js notation
proto.getView = function() {
var map = this.map;
var mapCenter = map.getCenter(),
center = { lon: mapCenter.lng, lat: mapCenter.lat };
return {
center: center,
zoom: map.getZoom(),
bearing: map.getBearing(),
pitch: map.getPitch()
};
};
function getStyleObj(val) {
var styleValues = layoutAttributes.style.values,
styleDflt = layoutAttributes.style.dflt,
styleObj = {};
if(Lib.isPlainObject(val)) {
styleObj.id = val.id;
styleObj.style = val;
}
else if(typeof val === 'string') {
styleObj.id = val;
styleObj.style = (styleValues.indexOf(val) !== -1) ?
convertStyleVal(val) :
val;
}
else {
styleObj.id = styleDflt;
styleObj.style = convertStyleVal(styleDflt);
}
return styleObj;
}
// if style is part of the 'official' mapbox values, add URL prefix and suffix
function convertStyleVal(val) {
return constants.styleUrlPrefix + val + '-' + constants.styleUrlSuffix;
}
function convertCenter(center) {
return [center.lon, center.lat];
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| area_attributes.js | 33.33% | (1 / 3) | 100% | (0 / 0) | 100% | (0 / 0) | 33.33% | (1 / 3) | |
| axis_attributes.js | 100% | (7 / 7) | 100% | (0 / 0) | 100% | (1 / 1) | 100% | (7 / 7) | |
| index.js | 100% | (2 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (2 / 2) | |
| micropolar.js | 6.88% | (54 / 785) | 0% | (0 / 333) | 0% | (0 / 159) | 7.51% | (54 / 719) | |
| micropolar_manager.js | 20.41% | (10 / 49) | 0% | (0 / 11) | 0% | (0 / 13) | 24.39% | (10 / 41) | |
| undo_manager.js | 5% | (2 / 40) | 0% | (0 / 12) | 0% | (0 / 12) | 5.88% | (2 / 34) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 14 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../../traces/scatter/attributes');
var scatterMarkerAttrs = scatterAttrs.marker;
module.exports = {
r: scatterAttrs.r,
t: scatterAttrs.t,
marker: {
color: scatterMarkerAttrs.color,
size: scatterMarkerAttrs.size,
symbol: scatterMarkerAttrs.symbol,
opacity: scatterMarkerAttrs.opacity
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 1 1 1 1 2 2 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var axesAttrs = require('../cartesian/layout_attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var domainAttr = extendFlat({}, axesAttrs.domain, {
description: [
'Polar chart subplots are not supported yet.',
'This key has currently no effect.'
].join(' ')
});
function mergeAttrs(axisName, nonCommonAttrs) {
var commonAttrs = {
showline: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not the line bounding this',
axisName, 'axis',
'will be shown on the figure.'
].join(' ')
},
showticklabels: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not the',
axisName, 'axis ticks',
'will feature tick labels.'
].join(' ')
},
tickorientation: {
valType: 'enumerated',
values: ['horizontal', 'vertical'],
role: 'style',
description: [
'Sets the orientation (from the paper perspective)',
'of the', axisName, 'axis tick labels.'
].join(' ')
},
ticklen: {
valType: 'number',
min: 0,
role: 'style',
description: [
'Sets the length of the tick lines on this', axisName, 'axis.'
].join(' ')
},
tickcolor: {
valType: 'color',
role: 'style',
description: [
'Sets the color of the tick lines on this', axisName, 'axis.'
].join(' ')
},
ticksuffix: {
valType: 'string',
role: 'style',
description: [
'Sets the length of the tick lines on this', axisName, 'axis.'
].join(' ')
},
endpadding: {
valType: 'number',
role: 'style'
},
visible: {
valType: 'boolean',
role: 'info',
description: [
'Determines whether or not this axis will be visible.'
].join(' ')
}
};
return extendFlat({}, nonCommonAttrs, commonAttrs);
}
module.exports = {
radialaxis: mergeAttrs('radial', {
range: {
valType: 'info_array',
role: 'info',
items: [
{ valType: 'number' },
{ valType: 'number' }
],
description: [
'Defines the start and end point of this radial axis.'
].join(' ')
},
domain: domainAttr,
orientation: {
valType: 'number',
role: 'style',
description: [
'Sets the orientation (an angle with respect to the origin)',
'of the radial axis.'
].join(' ')
}
}),
angularaxis: mergeAttrs('angular', {
range: {
valType: 'info_array',
role: 'info',
items: [
{ valType: 'number', dflt: 0 },
{ valType: 'number', dflt: 360 }
],
description: [
'Defines the start and end point of this angular axis.'
].join(' ')
},
domain: domainAttr
}),
// attributes that appear at layout root
layout: {
direction: {
valType: 'enumerated',
values: ['clockwise', 'counterclockwise'],
role: 'info',
description: [
'For polar plots only.',
'Sets the direction corresponding to positive angles.'
].join(' ')
},
orientation: {
valType: 'angle',
role: 'info',
description: [
'For polar plots only.',
'Rotates the entire polar by the given angle.'
].join(' ')
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Polar = module.exports = require('./micropolar');
Polar.manager = require('./micropolar_manager');
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var d3 = require('d3');
var Lib = require('../../lib');
var extendDeepAll = Lib.extendDeepAll;
var µ = module.exports = { version: '0.2.2' };
µ.Axis = function module() {
var config = {
data: [],
layout: {}
}, inputConfig = {}, liveConfig = {};
var svg, container, dispatch = d3.dispatch('hover'), radialScale, angularScale;
var exports = {};
function render(_container) {
container = _container || container;
var data = config.data;
var axisConfig = config.layout;
if (typeof container == 'string' || container.nodeName) container = d3.select(container);
container.datum(data).each(function(_data, _index) {
var dataOriginal = _data.slice();
liveConfig = {
data: µ.util.cloneJson(dataOriginal),
layout: µ.util.cloneJson(axisConfig)
};
var colorIndex = 0;
dataOriginal.forEach(function(d, i) {
if (!d.color) {
d.color = axisConfig.defaultColorRange[colorIndex];
colorIndex = (colorIndex + 1) % axisConfig.defaultColorRange.length;
}
if (!d.strokeColor) {
d.strokeColor = d.geometry === 'LinePlot' ? d.color : d3.rgb(d.color).darker().toString();
}
liveConfig.data[i].color = d.color;
liveConfig.data[i].strokeColor = d.strokeColor;
liveConfig.data[i].strokeDash = d.strokeDash;
liveConfig.data[i].strokeSize = d.strokeSize;
});
var data = dataOriginal.filter(function(d, i) {
var visible = d.visible;
return typeof visible === 'undefined' || visible === true;
});
var isStacked = false;
var dataWithGroupId = data.map(function(d, i) {
isStacked = isStacked || typeof d.groupId !== 'undefined';
return d;
});
if (isStacked) {
var grouped = d3.nest().key(function(d, i) {
return typeof d.groupId != 'undefined' ? d.groupId : 'unstacked';
}).entries(dataWithGroupId);
var dataYStack = [];
var stacked = grouped.map(function(d, i) {
if (d.key === 'unstacked') return d.values; else {
var prevArray = d.values[0].r.map(function(d, i) {
return 0;
});
d.values.forEach(function(d, i, a) {
d.yStack = [ prevArray ];
dataYStack.push(prevArray);
prevArray = µ.util.sumArrays(d.r, prevArray);
});
return d.values;
}
});
data = d3.merge(stacked);
}
data.forEach(function(d, i) {
d.t = Array.isArray(d.t[0]) ? d.t : [ d.t ];
d.r = Array.isArray(d.r[0]) ? d.r : [ d.r ];
});
var radius = Math.min(axisConfig.width - axisConfig.margin.left - axisConfig.margin.right, axisConfig.height - axisConfig.margin.top - axisConfig.margin.bottom) / 2;
radius = Math.max(10, radius);
var chartCenter = [ axisConfig.margin.left + radius, axisConfig.margin.top + radius ];
var extent;
if (isStacked) {
var highestStackedValue = d3.max(µ.util.sumArrays(µ.util.arrayLast(data).r[0], µ.util.arrayLast(dataYStack)));
extent = [ 0, highestStackedValue ];
} else extent = d3.extent(µ.util.flattenArray(data.map(function(d, i) {
return d.r;
})));
if (axisConfig.radialAxis.domain != µ.DATAEXTENT) extent[0] = 0;
radialScale = d3.scale.linear().domain(axisConfig.radialAxis.domain != µ.DATAEXTENT && axisConfig.radialAxis.domain ? axisConfig.radialAxis.domain : extent).range([ 0, radius ]);
liveConfig.layout.radialAxis.domain = radialScale.domain();
var angularDataMerged = µ.util.flattenArray(data.map(function(d, i) {
return d.t;
}));
var isOrdinal = typeof angularDataMerged[0] === 'string';
var ticks;
if (isOrdinal) {
angularDataMerged = µ.util.deduplicate(angularDataMerged);
ticks = angularDataMerged.slice();
angularDataMerged = d3.range(angularDataMerged.length);
data = data.map(function(d, i) {
var result = d;
d.t = [ angularDataMerged ];
if (isStacked) result.yStack = d.yStack;
return result;
});
}
var hasOnlyLineOrDotPlot = data.filter(function(d, i) {
return d.geometry === 'LinePlot' || d.geometry === 'DotPlot';
}).length === data.length;
var needsEndSpacing = axisConfig.needsEndSpacing === null ? isOrdinal || !hasOnlyLineOrDotPlot : axisConfig.needsEndSpacing;
var useProvidedDomain = axisConfig.angularAxis.domain && axisConfig.angularAxis.domain != µ.DATAEXTENT && !isOrdinal && axisConfig.angularAxis.domain[0] >= 0;
var angularDomain = useProvidedDomain ? axisConfig.angularAxis.domain : d3.extent(angularDataMerged);
var angularDomainStep = Math.abs(angularDataMerged[1] - angularDataMerged[0]);
if (hasOnlyLineOrDotPlot && !isOrdinal) angularDomainStep = 0;
var angularDomainWithPadding = angularDomain.slice();
if (needsEndSpacing && isOrdinal) angularDomainWithPadding[1] += angularDomainStep;
var tickCount = axisConfig.angularAxis.ticksCount || 4;
if (tickCount > 8) tickCount = tickCount / (tickCount / 8) + tickCount % 8;
if (axisConfig.angularAxis.ticksStep) {
tickCount = (angularDomainWithPadding[1] - angularDomainWithPadding[0]) / tickCount;
}
var angularTicksStep = axisConfig.angularAxis.ticksStep || (angularDomainWithPadding[1] - angularDomainWithPadding[0]) / (tickCount * (axisConfig.minorTicks + 1));
if (ticks) angularTicksStep = Math.max(Math.round(angularTicksStep), 1);
if (!angularDomainWithPadding[2]) angularDomainWithPadding[2] = angularTicksStep;
var angularAxisRange = d3.range.apply(this, angularDomainWithPadding);
angularAxisRange = angularAxisRange.map(function(d, i) {
return parseFloat(d.toPrecision(12));
});
angularScale = d3.scale.linear().domain(angularDomainWithPadding.slice(0, 2)).range(axisConfig.direction === 'clockwise' ? [ 0, 360 ] : [ 360, 0 ]);
liveConfig.layout.angularAxis.domain = angularScale.domain();
liveConfig.layout.angularAxis.endPadding = needsEndSpacing ? angularDomainStep : 0;
svg = d3.select(this).select('svg.chart-root');
if (typeof svg === 'undefined' || svg.empty()) {
var skeleton = "<svg xmlns='http://www.w3.org/2000/svg' class='chart-root'>' + '<g class='outer-group'>' + '<g class='chart-group'>' + '<circle class='background-circle'></circle>' + '<g class='geometry-group'></g>' + '<g class='radial axis-group'>' + '<circle class='outside-circle'></circle>' + '</g>' + '<g class='angular axis-group'></g>' + '<g class='guides-group'><line></line><circle r='0'></circle></g>' + '</g>' + '<g class='legend-group'></g>' + '<g class='tooltips-group'></g>' + '<g class='title-group'><text></text></g>' + '</g>' + '</svg>";
var doc = new DOMParser().parseFromString(skeleton, 'application/xml');
var newSvg = this.appendChild(this.ownerDocument.importNode(doc.documentElement, true));
svg = d3.select(newSvg);
}
svg.select('.guides-group').style({
'pointer-events': 'none'
});
svg.select('.angular.axis-group').style({
'pointer-events': 'none'
});
svg.select('.radial.axis-group').style({
'pointer-events': 'none'
});
var chartGroup = svg.select('.chart-group');
var lineStyle = {
fill: 'none',
stroke: axisConfig.tickColor
};
var fontStyle = {
'font-size': axisConfig.font.size,
'font-family': axisConfig.font.family,
fill: axisConfig.font.color,
'text-shadow': [ '-1px 0px', '1px -1px', '-1px 1px', '1px 1px' ].map(function(d, i) {
return ' ' + d + ' 0 ' + axisConfig.font.outlineColor;
}).join(',')
};
var legendContainer;
if (axisConfig.showLegend) {
legendContainer = svg.select('.legend-group').attr({
transform: 'translate(' + [ radius, axisConfig.margin.top ] + ')'
}).style({
display: 'block'
});
var elements = data.map(function(d, i) {
var datumClone = µ.util.cloneJson(d);
datumClone.symbol = d.geometry === 'DotPlot' ? d.dotType || 'circle' : d.geometry != 'LinePlot' ? 'square' : 'line';
datumClone.visibleInLegend = typeof d.visibleInLegend === 'undefined' || d.visibleInLegend;
datumClone.color = d.geometry === 'LinePlot' ? d.strokeColor : d.color;
return datumClone;
});
µ.Legend().config({
data: data.map(function(d, i) {
return d.name || 'Element' + i;
}),
legendConfig: extendDeepAll({},
µ.Legend.defaultConfig().legendConfig,
{
container: legendContainer,
elements: elements,
reverseOrder: axisConfig.legend.reverseOrder
}
)
})();
var legendBBox = legendContainer.node().getBBox();
radius = Math.min(axisConfig.width - legendBBox.width - axisConfig.margin.left - axisConfig.margin.right, axisConfig.height - axisConfig.margin.top - axisConfig.margin.bottom) / 2;
radius = Math.max(10, radius);
chartCenter = [ axisConfig.margin.left + radius, axisConfig.margin.top + radius ];
radialScale.range([ 0, radius ]);
liveConfig.layout.radialAxis.domain = radialScale.domain();
legendContainer.attr('transform', 'translate(' + [ chartCenter[0] + radius, chartCenter[1] - radius ] + ')');
} else {
legendContainer = svg.select('.legend-group').style({
display: 'none'
});
}
svg.attr({
width: axisConfig.width,
height: axisConfig.height
}).style({
opacity: axisConfig.opacity
});
chartGroup.attr('transform', 'translate(' + chartCenter + ')').style({
cursor: 'crosshair'
});
var centeringOffset = [ (axisConfig.width - (axisConfig.margin.left + axisConfig.margin.right + radius * 2 + (legendBBox ? legendBBox.width : 0))) / 2, (axisConfig.height - (axisConfig.margin.top + axisConfig.margin.bottom + radius * 2)) / 2 ];
centeringOffset[0] = Math.max(0, centeringOffset[0]);
centeringOffset[1] = Math.max(0, centeringOffset[1]);
svg.select('.outer-group').attr('transform', 'translate(' + centeringOffset + ')');
if (axisConfig.title) {
var title = svg.select('g.title-group text').style(fontStyle).text(axisConfig.title);
var titleBBox = title.node().getBBox();
title.attr({
x: chartCenter[0] - titleBBox.width / 2,
y: chartCenter[1] - radius - 20
});
}
var radialAxis = svg.select('.radial.axis-group');
if (axisConfig.radialAxis.gridLinesVisible) {
var gridCircles = radialAxis.selectAll('circle.grid-circle').data(radialScale.ticks(5));
gridCircles.enter().append('circle').attr({
'class': 'grid-circle'
}).style(lineStyle);
gridCircles.attr('r', radialScale);
gridCircles.exit().remove();
}
radialAxis.select('circle.outside-circle').attr({
r: radius
}).style(lineStyle);
var backgroundCircle = svg.select('circle.background-circle').attr({
r: radius
}).style({
fill: axisConfig.backgroundColor,
stroke: axisConfig.stroke
});
function currentAngle(d, i) {
return angularScale(d) % 360 + axisConfig.orientation;
}
if (axisConfig.radialAxis.visible) {
var axis = d3.svg.axis().scale(radialScale).ticks(5).tickSize(5);
radialAxis.call(axis).attr({
transform: 'rotate(' + axisConfig.radialAxis.orientation + ')'
});
radialAxis.selectAll('.domain').style(lineStyle);
radialAxis.selectAll('g>text').text(function(d, i) {
return this.textContent + axisConfig.radialAxis.ticksSuffix;
}).style(fontStyle).style({
'text-anchor': 'start'
}).attr({
x: 0,
y: 0,
dx: 0,
dy: 0,
transform: function(d, i) {
if (axisConfig.radialAxis.tickOrientation === 'horizontal') {
return 'rotate(' + -axisConfig.radialAxis.orientation + ') translate(' + [ 0, fontStyle['font-size'] ] + ')';
} else return 'translate(' + [ 0, fontStyle['font-size'] ] + ')';
}
});
radialAxis.selectAll('g>line').style({
stroke: 'black'
});
}
var angularAxis = svg.select('.angular.axis-group').selectAll('g.angular-tick').data(angularAxisRange);
var angularAxisEnter = angularAxis.enter().append('g').classed('angular-tick', true);
angularAxis.attr({
transform: function(d, i) {
return 'rotate(' + currentAngle(d, i) + ')';
}
}).style({
display: axisConfig.angularAxis.visible ? 'block' : 'none'
});
angularAxis.exit().remove();
angularAxisEnter.append('line').classed('grid-line', true).classed('major', function(d, i) {
return i % (axisConfig.minorTicks + 1) == 0;
}).classed('minor', function(d, i) {
return !(i % (axisConfig.minorTicks + 1) == 0);
}).style(lineStyle);
angularAxisEnter.selectAll('.minor').style({
stroke: axisConfig.minorTickColor
});
angularAxis.select('line.grid-line').attr({
x1: axisConfig.tickLength ? radius - axisConfig.tickLength : 0,
x2: radius
}).style({
display: axisConfig.angularAxis.gridLinesVisible ? 'block' : 'none'
});
angularAxisEnter.append('text').classed('axis-text', true).style(fontStyle);
var ticksText = angularAxis.select('text.axis-text').attr({
x: radius + axisConfig.labelOffset,
dy: '.35em',
transform: function(d, i) {
var angle = currentAngle(d, i);
var rad = radius + axisConfig.labelOffset;
var orient = axisConfig.angularAxis.tickOrientation;
if (orient == 'horizontal') return 'rotate(' + -angle + ' ' + rad + ' 0)'; else if (orient == 'radial') return angle < 270 && angle > 90 ? 'rotate(180 ' + rad + ' 0)' : null; else return 'rotate(' + (angle <= 180 && angle > 0 ? -90 : 90) + ' ' + rad + ' 0)';
}
}).style({
'text-anchor': 'middle',
display: axisConfig.angularAxis.labelsVisible ? 'block' : 'none'
}).text(function(d, i) {
if (i % (axisConfig.minorTicks + 1) != 0) return '';
if (ticks) {
return ticks[d] + axisConfig.angularAxis.ticksSuffix;
} else return d + axisConfig.angularAxis.ticksSuffix;
}).style(fontStyle);
if (axisConfig.angularAxis.rewriteTicks) ticksText.text(function(d, i) {
if (i % (axisConfig.minorTicks + 1) != 0) return '';
return axisConfig.angularAxis.rewriteTicks(this.textContent, i);
});
var rightmostTickEndX = d3.max(chartGroup.selectAll('.angular-tick text')[0].map(function(d, i) {
return d.getCTM().e + d.getBBox().width;
}));
legendContainer.attr({
transform: 'translate(' + [ radius + rightmostTickEndX, axisConfig.margin.top ] + ')'
});
var hasGeometry = svg.select('g.geometry-group').selectAll('g').size() > 0;
var geometryContainer = svg.select('g.geometry-group').selectAll('g.geometry').data(data);
geometryContainer.enter().append('g').attr({
'class': function(d, i) {
return 'geometry geometry' + i;
}
});
geometryContainer.exit().remove();
if (data[0] || hasGeometry) {
var geometryConfigs = [];
data.forEach(function(d, i) {
var geometryConfig = {};
geometryConfig.radialScale = radialScale;
geometryConfig.angularScale = angularScale;
geometryConfig.container = geometryContainer.filter(function(dB, iB) {
return iB == i;
});
geometryConfig.geometry = d.geometry;
geometryConfig.orientation = axisConfig.orientation;
geometryConfig.direction = axisConfig.direction;
geometryConfig.index = i;
geometryConfigs.push({
data: d,
geometryConfig: geometryConfig
});
});
var geometryConfigsGrouped = d3.nest().key(function(d, i) {
return typeof d.data.groupId != 'undefined' || 'unstacked';
}).entries(geometryConfigs);
var geometryConfigsGrouped2 = [];
geometryConfigsGrouped.forEach(function(d, i) {
if (d.key === 'unstacked') geometryConfigsGrouped2 = geometryConfigsGrouped2.concat(d.values.map(function(d, i) {
return [ d ];
})); else geometryConfigsGrouped2.push(d.values);
});
geometryConfigsGrouped2.forEach(function(d, i) {
var geometry;
if (Array.isArray(d)) geometry = d[0].geometryConfig.geometry; else geometry = d.geometryConfig.geometry;
var finalGeometryConfig = d.map(function(dB, iB) {
return extendDeepAll(µ[geometry].defaultConfig(), dB);
});
µ[geometry]().config(finalGeometryConfig)();
});
}
var guides = svg.select('.guides-group');
var tooltipContainer = svg.select('.tooltips-group');
var angularTooltip = µ.tooltipPanel().config({
container: tooltipContainer,
fontSize: 8
})();
var radialTooltip = µ.tooltipPanel().config({
container: tooltipContainer,
fontSize: 8
})();
var geometryTooltip = µ.tooltipPanel().config({
container: tooltipContainer,
hasTick: true
})();
var angularValue, radialValue;
if (!isOrdinal) {
var angularGuideLine = guides.select('line').attr({
x1: 0,
y1: 0,
y2: 0
}).style({
stroke: 'grey',
'pointer-events': 'none'
});
chartGroup.on('mousemove.angular-guide', function(d, i) {
var mouseAngle = µ.util.getMousePos(backgroundCircle).angle;
angularGuideLine.attr({
x2: -radius,
transform: 'rotate(' + mouseAngle + ')'
}).style({
opacity: .5
});
var angleWithOriginOffset = (mouseAngle + 180 + 360 - axisConfig.orientation) % 360;
angularValue = angularScale.invert(angleWithOriginOffset);
var pos = µ.util.convertToCartesian(radius + 12, mouseAngle + 180);
angularTooltip.text(µ.util.round(angularValue)).move([ pos[0] + chartCenter[0], pos[1] + chartCenter[1] ]);
}).on('mouseout.angular-guide', function(d, i) {
guides.select('line').style({
opacity: 0
});
});
}
var angularGuideCircle = guides.select('circle').style({
stroke: 'grey',
fill: 'none'
});
chartGroup.on('mousemove.radial-guide', function(d, i) {
var r = µ.util.getMousePos(backgroundCircle).radius;
angularGuideCircle.attr({
r: r
}).style({
opacity: .5
});
radialValue = radialScale.invert(µ.util.getMousePos(backgroundCircle).radius);
var pos = µ.util.convertToCartesian(r, axisConfig.radialAxis.orientation);
radialTooltip.text(µ.util.round(radialValue)).move([ pos[0] + chartCenter[0], pos[1] + chartCenter[1] ]);
}).on('mouseout.radial-guide', function(d, i) {
angularGuideCircle.style({
opacity: 0
});
geometryTooltip.hide();
angularTooltip.hide();
radialTooltip.hide();
});
svg.selectAll('.geometry-group .mark').on('mouseover.tooltip', function(d, i) {
var el = d3.select(this);
var color = el.style('fill');
var newColor = 'black';
var opacity = el.style('opacity') || 1;
el.attr({
'data-opacity': opacity
});
if (color != 'none') {
el.attr({
'data-fill': color
});
newColor = d3.hsl(color).darker().toString();
el.style({
fill: newColor,
opacity: 1
});
var textData = {
t: µ.util.round(d[0]),
r: µ.util.round(d[1])
};
if (isOrdinal) textData.t = ticks[d[0]];
var text = 't: ' + textData.t + ', r: ' + textData.r;
var bbox = this.getBoundingClientRect();
var svgBBox = svg.node().getBoundingClientRect();
var pos = [ bbox.left + bbox.width / 2 - centeringOffset[0] - svgBBox.left, bbox.top + bbox.height / 2 - centeringOffset[1] - svgBBox.top ];
geometryTooltip.config({
color: newColor
}).text(text);
geometryTooltip.move(pos);
} else {
color = el.style('stroke');
el.attr({
'data-stroke': color
});
newColor = d3.hsl(color).darker().toString();
el.style({
stroke: newColor,
opacity: 1
});
}
}).on('mousemove.tooltip', function(d, i) {
if (d3.event.which != 0) return false;
if (d3.select(this).attr('data-fill')) geometryTooltip.show();
}).on('mouseout.tooltip', function(d, i) {
geometryTooltip.hide();
var el = d3.select(this);
var fillColor = el.attr('data-fill');
if (fillColor) el.style({
fill: fillColor,
opacity: el.attr('data-opacity')
}); else el.style({
stroke: el.attr('data-stroke'),
opacity: el.attr('data-opacity')
});
});
});
return exports;
}
exports.render = function(_container) {
render(_container);
return this;
};
exports.config = function(_x) {
if (!arguments.length) return config;
var xClone = µ.util.cloneJson(_x);
xClone.data.forEach(function(d, i) {
if (!config.data[i]) config.data[i] = {};
extendDeepAll(config.data[i], µ.Axis.defaultConfig().data[0]);
extendDeepAll(config.data[i], d);
});
extendDeepAll(config.layout, µ.Axis.defaultConfig().layout);
extendDeepAll(config.layout, xClone.layout);
return this;
};
exports.getLiveConfig = function() {
return liveConfig;
};
exports.getinputConfig = function() {
return inputConfig;
};
exports.radialScale = function(_x) {
return radialScale;
};
exports.angularScale = function(_x) {
return angularScale;
};
exports.svg = function() {
return svg;
};
d3.rebind(exports, dispatch, 'on');
return exports;
};
µ.Axis.defaultConfig = function(d, i) {
var config = {
data: [ {
t: [ 1, 2, 3, 4 ],
r: [ 10, 11, 12, 13 ],
name: 'Line1',
geometry: 'LinePlot',
color: null,
strokeDash: 'solid',
strokeColor: null,
strokeSize: '1',
visibleInLegend: true,
opacity: 1
} ],
layout: {
defaultColorRange: d3.scale.category10().range(),
title: null,
height: 450,
width: 500,
margin: {
top: 40,
right: 40,
bottom: 40,
left: 40
},
font: {
size: 12,
color: 'gray',
outlineColor: 'white',
family: 'Tahoma, sans-serif'
},
direction: 'clockwise',
orientation: 0,
labelOffset: 10,
radialAxis: {
domain: null,
orientation: -45,
ticksSuffix: '',
visible: true,
gridLinesVisible: true,
tickOrientation: 'horizontal',
rewriteTicks: null
},
angularAxis: {
domain: [ 0, 360 ],
ticksSuffix: '',
visible: true,
gridLinesVisible: true,
labelsVisible: true,
tickOrientation: 'horizontal',
rewriteTicks: null,
ticksCount: null,
ticksStep: null
},
minorTicks: 0,
tickLength: null,
tickColor: 'silver',
minorTickColor: '#eee',
backgroundColor: 'none',
needsEndSpacing: null,
showLegend: true,
legend: {
reverseOrder: false
},
opacity: 1
}
};
return config;
};
µ.util = {};
µ.DATAEXTENT = 'dataExtent';
µ.AREA = 'AreaChart';
µ.LINE = 'LinePlot';
µ.DOT = 'DotPlot';
µ.BAR = 'BarChart';
µ.util._override = function(_objA, _objB) {
for (var x in _objA) if (x in _objB) _objB[x] = _objA[x];
};
µ.util._extend = function(_objA, _objB) {
for (var x in _objA) _objB[x] = _objA[x];
};
µ.util._rndSnd = function() {
return Math.random() * 2 - 1 + (Math.random() * 2 - 1) + (Math.random() * 2 - 1);
};
µ.util.dataFromEquation2 = function(_equation, _step) {
var step = _step || 6;
var data = d3.range(0, 360 + step, step).map(function(deg, index) {
var theta = deg * Math.PI / 180;
var radius = _equation(theta);
return [ deg, radius ];
});
return data;
};
µ.util.dataFromEquation = function(_equation, _step, _name) {
var step = _step || 6;
var t = [], r = [];
d3.range(0, 360 + step, step).forEach(function(deg, index) {
var theta = deg * Math.PI / 180;
var radius = _equation(theta);
t.push(deg);
r.push(radius);
});
var result = {
t: t,
r: r
};
if (_name) result.name = _name;
return result;
};
µ.util.ensureArray = function(_val, _count) {
if (typeof _val === 'undefined') return null;
var arr = [].concat(_val);
return d3.range(_count).map(function(d, i) {
return arr[i] || arr[0];
});
};
µ.util.fillArrays = function(_obj, _valueNames, _count) {
_valueNames.forEach(function(d, i) {
_obj[d] = µ.util.ensureArray(_obj[d], _count);
});
return _obj;
};
µ.util.cloneJson = function(json) {
return JSON.parse(JSON.stringify(json));
};
µ.util.validateKeys = function(obj, keys) {
if (typeof keys === 'string') keys = keys.split('.');
var next = keys.shift();
return obj[next] && (!keys.length || objHasKeys(obj[next], keys));
};
µ.util.sumArrays = function(a, b) {
return d3.zip(a, b).map(function(d, i) {
return d3.sum(d);
});
};
µ.util.arrayLast = function(a) {
return a[a.length - 1];
};
µ.util.arrayEqual = function(a, b) {
var i = Math.max(a.length, b.length, 1);
while (i-- >= 0 && a[i] === b[i]) ;
return i === -2;
};
µ.util.flattenArray = function(arr) {
var r = [];
while (!µ.util.arrayEqual(r, arr)) {
r = arr;
arr = [].concat.apply([], arr);
}
return arr;
};
µ.util.deduplicate = function(arr) {
return arr.filter(function(v, i, a) {
return a.indexOf(v) == i;
});
};
µ.util.convertToCartesian = function(radius, theta) {
var thetaRadians = theta * Math.PI / 180;
var x = radius * Math.cos(thetaRadians);
var y = radius * Math.sin(thetaRadians);
return [ x, y ];
};
µ.util.round = function(_value, _digits) {
var digits = _digits || 2;
var mult = Math.pow(10, digits);
return Math.round(_value * mult) / mult;
};
µ.util.getMousePos = function(_referenceElement) {
var mousePos = d3.mouse(_referenceElement.node());
var mouseX = mousePos[0];
var mouseY = mousePos[1];
var mouse = {};
mouse.x = mouseX;
mouse.y = mouseY;
mouse.pos = mousePos;
mouse.angle = (Math.atan2(mouseY, mouseX) + Math.PI) * 180 / Math.PI;
mouse.radius = Math.sqrt(mouseX * mouseX + mouseY * mouseY);
return mouse;
};
µ.util.duplicatesCount = function(arr) {
var uniques = {}, val;
var dups = {};
for (var i = 0, len = arr.length; i < len; i++) {
val = arr[i];
if (val in uniques) {
uniques[val]++;
dups[val] = uniques[val];
} else {
uniques[val] = 1;
}
}
return dups;
};
µ.util.duplicates = function(arr) {
return Object.keys(µ.util.duplicatesCount(arr));
};
µ.util.translator = function(obj, sourceBranch, targetBranch, reverse) {
if (reverse) {
var targetBranchCopy = targetBranch.slice();
targetBranch = sourceBranch;
sourceBranch = targetBranchCopy;
}
var value = sourceBranch.reduce(function(previousValue, currentValue) {
if (typeof previousValue != 'undefined') return previousValue[currentValue];
}, obj);
if (typeof value === 'undefined') return;
sourceBranch.reduce(function(previousValue, currentValue, index) {
if (typeof previousValue == 'undefined') return;
if (index === sourceBranch.length - 1) delete previousValue[currentValue];
return previousValue[currentValue];
}, obj);
targetBranch.reduce(function(previousValue, currentValue, index) {
if (typeof previousValue[currentValue] === 'undefined') previousValue[currentValue] = {};
if (index === targetBranch.length - 1) previousValue[currentValue] = value;
return previousValue[currentValue];
}, obj);
};
µ.PolyChart = function module() {
var config = [ µ.PolyChart.defaultConfig() ];
var dispatch = d3.dispatch('hover');
var dashArray = {
solid: 'none',
dash: [ 5, 2 ],
dot: [ 2, 5 ]
};
var colorScale;
function exports() {
var geometryConfig = config[0].geometryConfig;
var container = geometryConfig.container;
if (typeof container == 'string') container = d3.select(container);
container.datum(config).each(function(_config, _index) {
var isStack = !!_config[0].data.yStack;
var data = _config.map(function(d, i) {
if (isStack) return d3.zip(d.data.t[0], d.data.r[0], d.data.yStack[0]); else return d3.zip(d.data.t[0], d.data.r[0]);
});
var angularScale = geometryConfig.angularScale;
var domainMin = geometryConfig.radialScale.domain()[0];
var generator = {};
generator.bar = function(d, i, pI) {
var dataConfig = _config[pI].data;
var h = geometryConfig.radialScale(d[1]) - geometryConfig.radialScale(0);
var stackTop = geometryConfig.radialScale(d[2] || 0);
var w = dataConfig.barWidth;
d3.select(this).attr({
'class': 'mark bar',
d: 'M' + [ [ h + stackTop, -w / 2 ], [ h + stackTop, w / 2 ], [ stackTop, w / 2 ], [ stackTop, -w / 2 ] ].join('L') + 'Z',
transform: function(d, i) {
return 'rotate(' + (geometryConfig.orientation + angularScale(d[0])) + ')';
}
});
};
generator.dot = function(d, i, pI) {
var stackedData = d[2] ? [ d[0], d[1] + d[2] ] : d;
var symbol = d3.svg.symbol().size(_config[pI].data.dotSize).type(_config[pI].data.dotType)(d, i);
d3.select(this).attr({
'class': 'mark dot',
d: symbol,
transform: function(d, i) {
var coord = convertToCartesian(getPolarCoordinates(stackedData));
return 'translate(' + [ coord.x, coord.y ] + ')';
}
});
};
var line = d3.svg.line.radial().interpolate(_config[0].data.lineInterpolation).radius(function(d) {
return geometryConfig.radialScale(d[1]);
}).angle(function(d) {
return geometryConfig.angularScale(d[0]) * Math.PI / 180;
});
generator.line = function(d, i, pI) {
var lineData = d[2] ? data[pI].map(function(d, i) {
return [ d[0], d[1] + d[2] ];
}) : data[pI];
d3.select(this).each(generator['dot']).style({
opacity: function(dB, iB) {
return +_config[pI].data.dotVisible;
},
fill: markStyle.stroke(d, i, pI)
}).attr({
'class': 'mark dot'
});
if (i > 0) return;
var lineSelection = d3.select(this.parentNode).selectAll('path.line').data([ 0 ]);
lineSelection.enter().insert('path');
lineSelection.attr({
'class': 'line',
d: line(lineData),
transform: function(dB, iB) {
return 'rotate(' + (geometryConfig.orientation + 90) + ')';
},
'pointer-events': 'none'
}).style({
fill: function(dB, iB) {
return markStyle.fill(d, i, pI);
},
'fill-opacity': 0,
stroke: function(dB, iB) {
return markStyle.stroke(d, i, pI);
},
'stroke-width': function(dB, iB) {
return markStyle['stroke-width'](d, i, pI);
},
'stroke-dasharray': function(dB, iB) {
return markStyle['stroke-dasharray'](d, i, pI);
},
opacity: function(dB, iB) {
return markStyle.opacity(d, i, pI);
},
display: function(dB, iB) {
return markStyle.display(d, i, pI);
}
});
};
var angularRange = geometryConfig.angularScale.range();
var triangleAngle = Math.abs(angularRange[1] - angularRange[0]) / data[0].length * Math.PI / 180;
var arc = d3.svg.arc().startAngle(function(d) {
return -triangleAngle / 2;
}).endAngle(function(d) {
return triangleAngle / 2;
}).innerRadius(function(d) {
return geometryConfig.radialScale(domainMin + (d[2] || 0));
}).outerRadius(function(d) {
return geometryConfig.radialScale(domainMin + (d[2] || 0)) + geometryConfig.radialScale(d[1]);
});
generator.arc = function(d, i, pI) {
d3.select(this).attr({
'class': 'mark arc',
d: arc,
transform: function(d, i) {
return 'rotate(' + (geometryConfig.orientation + angularScale(d[0]) + 90) + ')';
}
});
};
var markStyle = {
fill: function(d, i, pI) {
return _config[pI].data.color;
},
stroke: function(d, i, pI) {
return _config[pI].data.strokeColor;
},
'stroke-width': function(d, i, pI) {
return _config[pI].data.strokeSize + 'px';
},
'stroke-dasharray': function(d, i, pI) {
return dashArray[_config[pI].data.strokeDash];
},
opacity: function(d, i, pI) {
return _config[pI].data.opacity;
},
display: function(d, i, pI) {
return typeof _config[pI].data.visible === 'undefined' || _config[pI].data.visible ? 'block' : 'none';
}
};
var geometryLayer = d3.select(this).selectAll('g.layer').data(data);
geometryLayer.enter().append('g').attr({
'class': 'layer'
});
var geometry = geometryLayer.selectAll('path.mark').data(function(d, i) {
return d;
});
geometry.enter().append('path').attr({
'class': 'mark'
});
geometry.style(markStyle).each(generator[geometryConfig.geometryType]);
geometry.exit().remove();
geometryLayer.exit().remove();
function getPolarCoordinates(d, i) {
var r = geometryConfig.radialScale(d[1]);
var t = (geometryConfig.angularScale(d[0]) + geometryConfig.orientation) * Math.PI / 180;
return {
r: r,
t: t
};
}
function convertToCartesian(polarCoordinates) {
var x = polarCoordinates.r * Math.cos(polarCoordinates.t);
var y = polarCoordinates.r * Math.sin(polarCoordinates.t);
return {
x: x,
y: y
};
}
});
}
exports.config = function(_x) {
if (!arguments.length) return config;
_x.forEach(function(d, i) {
if (!config[i]) config[i] = {};
extendDeepAll(config[i], µ.PolyChart.defaultConfig());
extendDeepAll(config[i], d);
});
return this;
};
exports.getColorScale = function() {
return colorScale;
};
d3.rebind(exports, dispatch, 'on');
return exports;
};
µ.PolyChart.defaultConfig = function() {
var config = {
data: {
name: 'geom1',
t: [ [ 1, 2, 3, 4 ] ],
r: [ [ 1, 2, 3, 4 ] ],
dotType: 'circle',
dotSize: 64,
dotVisible: false,
barWidth: 20,
color: '#ffa500',
strokeSize: 1,
strokeColor: 'silver',
strokeDash: 'solid',
opacity: 1,
index: 0,
visible: true,
visibleInLegend: true
},
geometryConfig: {
geometry: 'LinePlot',
geometryType: 'arc',
direction: 'clockwise',
orientation: 0,
container: 'body',
radialScale: null,
angularScale: null,
colorScale: d3.scale.category20()
}
};
return config;
};
µ.BarChart = function module() {
return µ.PolyChart();
};
µ.BarChart.defaultConfig = function() {
var config = {
geometryConfig: {
geometryType: 'bar'
}
};
return config;
};
µ.AreaChart = function module() {
return µ.PolyChart();
};
µ.AreaChart.defaultConfig = function() {
var config = {
geometryConfig: {
geometryType: 'arc'
}
};
return config;
};
µ.DotPlot = function module() {
return µ.PolyChart();
};
µ.DotPlot.defaultConfig = function() {
var config = {
geometryConfig: {
geometryType: 'dot',
dotType: 'circle'
}
};
return config;
};
µ.LinePlot = function module() {
return µ.PolyChart();
};
µ.LinePlot.defaultConfig = function() {
var config = {
geometryConfig: {
geometryType: 'line'
}
};
return config;
};
µ.Legend = function module() {
var config = µ.Legend.defaultConfig();
var dispatch = d3.dispatch('hover');
function exports() {
var legendConfig = config.legendConfig;
var flattenData = config.data.map(function(d, i) {
return [].concat(d).map(function(dB, iB) {
var element = extendDeepAll({}, legendConfig.elements[i]);
element.name = dB;
element.color = [].concat(legendConfig.elements[i].color)[iB];
return element;
});
});
var data = d3.merge(flattenData);
data = data.filter(function(d, i) {
return legendConfig.elements[i] && (legendConfig.elements[i].visibleInLegend || typeof legendConfig.elements[i].visibleInLegend === 'undefined');
});
if (legendConfig.reverseOrder) data = data.reverse();
var container = legendConfig.container;
if (typeof container == 'string' || container.nodeName) container = d3.select(container);
var colors = data.map(function(d, i) {
return d.color;
});
var lineHeight = legendConfig.fontSize;
var isContinuous = legendConfig.isContinuous == null ? typeof data[0] === 'number' : legendConfig.isContinuous;
var height = isContinuous ? legendConfig.height : lineHeight * data.length;
var legendContainerGroup = container.classed('legend-group', true);
var svg = legendContainerGroup.selectAll('svg').data([ 0 ]);
var svgEnter = svg.enter().append('svg').attr({
width: 300,
height: height + lineHeight,
xmlns: 'http://www.w3.org/2000/svg',
'xmlns:xlink': 'http://www.w3.org/1999/xlink',
version: '1.1'
});
svgEnter.append('g').classed('legend-axis', true);
svgEnter.append('g').classed('legend-marks', true);
var dataNumbered = d3.range(data.length);
var colorScale = d3.scale[isContinuous ? 'linear' : 'ordinal']().domain(dataNumbered).range(colors);
var dataScale = d3.scale[isContinuous ? 'linear' : 'ordinal']().domain(dataNumbered)[isContinuous ? 'range' : 'rangePoints']([ 0, height ]);
var shapeGenerator = function(_type, _size) {
var squareSize = _size * 3;
if (_type === 'line') {
return 'M' + [ [ -_size / 2, -_size / 12 ], [ _size / 2, -_size / 12 ], [ _size / 2, _size / 12 ], [ -_size / 2, _size / 12 ] ] + 'Z';
} else if (d3.svg.symbolTypes.indexOf(_type) != -1) return d3.svg.symbol().type(_type).size(squareSize)(); else return d3.svg.symbol().type('square').size(squareSize)();
};
if (isContinuous) {
var gradient = svg.select('.legend-marks').append('defs').append('linearGradient').attr({
id: 'grad1',
x1: '0%',
y1: '0%',
x2: '0%',
y2: '100%'
}).selectAll('stop').data(colors);
gradient.enter().append('stop');
gradient.attr({
offset: function(d, i) {
return i / (colors.length - 1) * 100 + '%';
}
}).style({
'stop-color': function(d, i) {
return d;
}
});
svg.append('rect').classed('legend-mark', true).attr({
height: legendConfig.height,
width: legendConfig.colorBandWidth,
fill: 'url(#grad1)'
});
} else {
var legendElement = svg.select('.legend-marks').selectAll('path.legend-mark').data(data);
legendElement.enter().append('path').classed('legend-mark', true);
legendElement.attr({
transform: function(d, i) {
return 'translate(' + [ lineHeight / 2, dataScale(i) + lineHeight / 2 ] + ')';
},
d: function(d, i) {
var symbolType = d.symbol;
return shapeGenerator(symbolType, lineHeight);
},
fill: function(d, i) {
return colorScale(i);
}
});
legendElement.exit().remove();
}
var legendAxis = d3.svg.axis().scale(dataScale).orient('right');
var axis = svg.select('g.legend-axis').attr({
transform: 'translate(' + [ isContinuous ? legendConfig.colorBandWidth : lineHeight, lineHeight / 2 ] + ')'
}).call(legendAxis);
axis.selectAll('.domain').style({
fill: 'none',
stroke: 'none'
});
axis.selectAll('line').style({
fill: 'none',
stroke: isContinuous ? legendConfig.textColor : 'none'
});
axis.selectAll('text').style({
fill: legendConfig.textColor,
'font-size': legendConfig.fontSize
}).text(function(d, i) {
return data[i].name;
});
return exports;
}
exports.config = function(_x) {
if (!arguments.length) return config;
extendDeepAll(config, _x);
return this;
};
d3.rebind(exports, dispatch, 'on');
return exports;
};
µ.Legend.defaultConfig = function(d, i) {
var config = {
data: [ 'a', 'b', 'c' ],
legendConfig: {
elements: [ {
symbol: 'line',
color: 'red'
}, {
symbol: 'square',
color: 'yellow'
}, {
symbol: 'diamond',
color: 'limegreen'
} ],
height: 150,
colorBandWidth: 30,
fontSize: 12,
container: 'body',
isContinuous: null,
textColor: 'grey',
reverseOrder: false
}
};
return config;
};
µ.tooltipPanel = function() {
var tooltipEl, tooltipTextEl, backgroundEl;
var config = {
container: null,
hasTick: false,
fontSize: 12,
color: 'white',
padding: 5
};
var id = 'tooltip-' + µ.tooltipPanel.uid++;
var tickSize = 10;
var exports = function() {
tooltipEl = config.container.selectAll('g.' + id).data([ 0 ]);
var tooltipEnter = tooltipEl.enter().append('g').classed(id, true).style({
'pointer-events': 'none',
display: 'none'
});
backgroundEl = tooltipEnter.append('path').style({
fill: 'white',
'fill-opacity': .9
}).attr({
d: 'M0 0'
});
tooltipTextEl = tooltipEnter.append('text').attr({
dx: config.padding + tickSize,
dy: +config.fontSize * .3
});
return exports;
};
exports.text = function(_text) {
var l = d3.hsl(config.color).l;
var strokeColor = l >= .5 ? '#aaa' : 'white';
var fillColor = l >= .5 ? 'black' : 'white';
var text = _text || '';
tooltipTextEl.style({
fill: fillColor,
'font-size': config.fontSize + 'px'
}).text(text);
var padding = config.padding;
var bbox = tooltipTextEl.node().getBBox();
var boxStyle = {
fill: config.color,
stroke: strokeColor,
'stroke-width': '2px'
};
var backGroundW = bbox.width + padding * 2 + tickSize;
var backGroundH = bbox.height + padding * 2;
backgroundEl.attr({
d: 'M' + [ [ tickSize, -backGroundH / 2 ], [ tickSize, -backGroundH / 4 ], [ config.hasTick ? 0 : tickSize, 0 ], [ tickSize, backGroundH / 4 ], [ tickSize, backGroundH / 2 ], [ backGroundW, backGroundH / 2 ], [ backGroundW, -backGroundH / 2 ] ].join('L') + 'Z'
}).style(boxStyle);
tooltipEl.attr({
transform: 'translate(' + [ tickSize, -backGroundH / 2 + padding * 2 ] + ')'
});
tooltipEl.style({
display: 'block'
});
return exports;
};
exports.move = function(_pos) {
if (!tooltipEl) return;
tooltipEl.attr({
transform: 'translate(' + [ _pos[0], _pos[1] ] + ')'
}).style({
display: 'block'
});
return exports;
};
exports.hide = function() {
if (!tooltipEl) return;
tooltipEl.style({
display: 'none'
});
return exports;
};
exports.show = function() {
if (!tooltipEl) return;
tooltipEl.style({
display: 'block'
});
return exports;
};
exports.config = function(_x) {
extendDeepAll(config, _x);
return exports;
};
return exports;
};
µ.tooltipPanel.uid = 1;
µ.adapter = {};
µ.adapter.plotly = function module() {
var exports = {};
exports.convert = function(_inputConfig, reverse) {
var outputConfig = {};
if (_inputConfig.data) {
outputConfig.data = _inputConfig.data.map(function(d, i) {
var r = extendDeepAll({}, d);
var toTranslate = [
[ r, [ 'marker', 'color' ], [ 'color' ] ],
[ r, [ 'marker', 'opacity' ], [ 'opacity' ] ],
[ r, [ 'marker', 'line', 'color' ], [ 'strokeColor' ] ],
[ r, [ 'marker', 'line', 'dash' ], [ 'strokeDash' ] ],
[ r, [ 'marker', 'line', 'width' ], [ 'strokeSize' ] ],
[ r, [ 'marker', 'symbol' ], [ 'dotType' ] ],
[ r, [ 'marker', 'size' ], [ 'dotSize' ] ],
[ r, [ 'marker', 'barWidth' ], [ 'barWidth' ] ],
[ r, [ 'line', 'interpolation' ], [ 'lineInterpolation' ] ],
[ r, [ 'showlegend' ], [ 'visibleInLegend' ] ]
];
toTranslate.forEach(function(d, i) {
µ.util.translator.apply(null, d.concat(reverse));
});
if (!reverse) delete r.marker;
if (reverse) delete r.groupId;
if (!reverse) {
if (r.type === 'scatter') {
if (r.mode === 'lines') r.geometry = 'LinePlot'; else if (r.mode === 'markers') r.geometry = 'DotPlot'; else if (r.mode === 'lines+markers') {
r.geometry = 'LinePlot';
r.dotVisible = true;
}
} else if (r.type === 'area') r.geometry = 'AreaChart'; else if (r.type === 'bar') r.geometry = 'BarChart';
delete r.mode;
delete r.type;
} else {
if (r.geometry === 'LinePlot') {
r.type = 'scatter';
if (r.dotVisible === true) {
delete r.dotVisible;
r.mode = 'lines+markers';
} else r.mode = 'lines';
} else if (r.geometry === 'DotPlot') {
r.type = 'scatter';
r.mode = 'markers';
} else if (r.geometry === 'AreaChart') r.type = 'area'; else if (r.geometry === 'BarChart') r.type = 'bar';
delete r.geometry;
}
return r;
});
if (!reverse && _inputConfig.layout && _inputConfig.layout.barmode === 'stack') {
var duplicates = µ.util.duplicates(outputConfig.data.map(function(d, i) {
return d.geometry;
}));
outputConfig.data.forEach(function(d, i) {
var idx = duplicates.indexOf(d.geometry);
if (idx != -1) outputConfig.data[i].groupId = idx;
});
}
}
if (_inputConfig.layout) {
var r = extendDeepAll({}, _inputConfig.layout);
var toTranslate = [
[ r, [ 'plot_bgcolor' ], [ 'backgroundColor' ] ],
[ r, [ 'showlegend' ], [ 'showLegend' ] ],
[ r, [ 'radialaxis' ], [ 'radialAxis' ] ],
[ r, [ 'angularaxis' ], [ 'angularAxis' ] ],
[ r.angularaxis, [ 'showline' ], [ 'gridLinesVisible' ] ],
[ r.angularaxis, [ 'showticklabels' ], [ 'labelsVisible' ] ],
[ r.angularaxis, [ 'nticks' ], [ 'ticksCount' ] ],
[ r.angularaxis, [ 'tickorientation' ], [ 'tickOrientation' ] ],
[ r.angularaxis, [ 'ticksuffix' ], [ 'ticksSuffix' ] ],
[ r.angularaxis, [ 'range' ], [ 'domain' ] ],
[ r.angularaxis, [ 'endpadding' ], [ 'endPadding' ] ],
[ r.radialaxis, [ 'showline' ], [ 'gridLinesVisible' ] ],
[ r.radialaxis, [ 'tickorientation' ], [ 'tickOrientation' ] ],
[ r.radialaxis, [ 'ticksuffix' ], [ 'ticksSuffix' ] ],
[ r.radialaxis, [ 'range' ], [ 'domain' ] ],
[ r.angularAxis, [ 'showline' ], [ 'gridLinesVisible' ] ],
[ r.angularAxis, [ 'showticklabels' ], [ 'labelsVisible' ] ],
[ r.angularAxis, [ 'nticks' ], [ 'ticksCount' ] ],
[ r.angularAxis, [ 'tickorientation' ], [ 'tickOrientation' ] ],
[ r.angularAxis, [ 'ticksuffix' ], [ 'ticksSuffix' ] ],
[ r.angularAxis, [ 'range' ], [ 'domain' ] ],
[ r.angularAxis, [ 'endpadding' ], [ 'endPadding' ] ],
[ r.radialAxis, [ 'showline' ], [ 'gridLinesVisible' ] ],
[ r.radialAxis, [ 'tickorientation' ], [ 'tickOrientation' ] ],
[ r.radialAxis, [ 'ticksuffix' ], [ 'ticksSuffix' ] ],
[ r.radialAxis, [ 'range' ], [ 'domain' ] ],
[ r.font, [ 'outlinecolor' ], [ 'outlineColor' ] ],
[ r.legend, [ 'traceorder' ], [ 'reverseOrder' ] ],
[ r, [ 'labeloffset' ], [ 'labelOffset' ] ],
[ r, [ 'defaultcolorrange' ], [ 'defaultColorRange' ] ]
];
toTranslate.forEach(function(d, i) {
µ.util.translator.apply(null, d.concat(reverse));
});
if (!reverse) {
if (r.angularAxis && typeof r.angularAxis.ticklen !== 'undefined') r.tickLength = r.angularAxis.ticklen;
if (r.angularAxis && typeof r.angularAxis.tickcolor !== 'undefined') r.tickColor = r.angularAxis.tickcolor;
} else {
if (typeof r.tickLength !== 'undefined') {
r.angularaxis.ticklen = r.tickLength;
delete r.tickLength;
}
if (r.tickColor) {
r.angularaxis.tickcolor = r.tickColor;
delete r.tickColor;
}
}
if (r.legend && typeof r.legend.reverseOrder != 'boolean') {
r.legend.reverseOrder = r.legend.reverseOrder != 'normal';
}
if (r.legend && typeof r.legend.traceorder == 'boolean') {
r.legend.traceorder = r.legend.traceorder ? 'reversed' : 'normal';
delete r.legend.reverseOrder;
}
if (r.margin && typeof r.margin.t != 'undefined') {
var source = [ 't', 'r', 'b', 'l', 'pad' ];
var target = [ 'top', 'right', 'bottom', 'left', 'pad' ];
var margin = {};
d3.entries(r.margin).forEach(function(dB, iB) {
margin[target[source.indexOf(dB.key)]] = dB.value;
});
r.margin = margin;
}
if (reverse) {
delete r.needsEndSpacing;
delete r.minorTickColor;
delete r.minorTicks;
delete r.angularaxis.ticksCount;
delete r.angularaxis.ticksCount;
delete r.angularaxis.ticksStep;
delete r.angularaxis.rewriteTicks;
delete r.angularaxis.nticks;
delete r.radialaxis.ticksCount;
delete r.radialaxis.ticksCount;
delete r.radialaxis.ticksStep;
delete r.radialaxis.rewriteTicks;
delete r.radialaxis.nticks;
}
outputConfig.layout = r;
}
return outputConfig;
};
return exports;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* eslint-disable new-cap */
'use strict';
var d3 = require('d3');
var Lib = require('../../lib');
var Color = require('../../components/color');
var micropolar = require('./micropolar');
var UndoManager = require('./undo_manager');
var extendDeepAll = Lib.extendDeepAll;
var manager = module.exports = {};
manager.framework = function(_gd) {
var config, previousConfigClone, plot, convertedInput, container;
var undoManager = new UndoManager();
function exports(_inputConfig, _container) {
if(_container) container = _container;
d3.select(d3.select(container).node().parentNode).selectAll('.svg-container>*:not(.chart-root)').remove();
config = (!config) ?
_inputConfig :
extendDeepAll(config, _inputConfig);
if(!plot) plot = micropolar.Axis();
convertedInput = micropolar.adapter.plotly().convert(config);
plot.config(convertedInput).render(container);
_gd.data = config.data;
_gd.layout = config.layout;
manager.fillLayout(_gd);
return config;
}
exports.isPolar = true;
exports.svg = function() { return plot.svg(); };
exports.getConfig = function() { return config; };
exports.getLiveConfig = function() {
return micropolar.adapter.plotly().convert(plot.getLiveConfig(), true);
};
exports.getLiveScales = function() { return {t: plot.angularScale(), r: plot.radialScale()}; };
exports.setUndoPoint = function() {
var that = this;
var configClone = micropolar.util.cloneJson(config);
(function(_configClone, _previousConfigClone) {
undoManager.add({
undo: function() {
if(_previousConfigClone) that(_previousConfigClone);
},
redo: function() {
that(_configClone);
}
});
})(configClone, previousConfigClone);
previousConfigClone = micropolar.util.cloneJson(configClone);
};
exports.undo = function() { undoManager.undo(); };
exports.redo = function() { undoManager.redo(); };
return exports;
};
manager.fillLayout = function(_gd) {
var container = d3.select(_gd).selectAll('.plot-container'),
paperDiv = container.selectAll('.svg-container'),
paper = _gd.framework && _gd.framework.svg && _gd.framework.svg(),
dflts = {
width: 800,
height: 600,
paper_bgcolor: Color.background,
_container: container,
_paperdiv: paperDiv,
_paper: paper
};
_gd._fullLayout = extendDeepAll(dflts, _gd.layout);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // Modified from https://github.com/ArthurClemens/Javascript-Undo-Manager // Copyright (c) 2010-2013 Arthur Clemens, arthur@visiblearea.com module.exports = function UndoManager() { var undoCommands = [], index = -1, isExecuting = false, callback; function execute(command, action) { if(!command) return this; isExecuting = true; command[action](); isExecuting = false; return this; } return { add: function(command) { if(isExecuting) return this; undoCommands.splice(index + 1, undoCommands.length - index); undoCommands.push(command); index = undoCommands.length - 1; return this; }, setCallback: function(callbackFunc) { callback = callbackFunc; }, undo: function() { var command = undoCommands[index]; if(!command) return this; execute(command, 'undo'); index -= 1; if(callback) callback(command.undo); return this; }, redo: function() { var command = undoCommands[index + 1]; if(!command) return this; execute(command, 'redo'); index += 1; if(callback) callback(command.redo); return this; }, clear: function() { undoCommands = []; index = -1; }, hasUndo: function() { return index !== -1; }, hasRedo: function() { return index < (undoCommands.length - 1); }, getCommands: function() { return undoCommands; }, getPreviousCommand: function() { return undoCommands[index - 1]; }, getIndex: function() { return index; } }; }; |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| ternary.js | 16.41% | (43 / 262) | 0% | (0 / 87) | 0% | (0 / 28) | 17.13% | (43 / 251) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var tinycolor = require('tinycolor2');
var Plotly = require('../../plotly');
var Lib = require('../../lib');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var setConvert = require('../cartesian/set_convert');
var extendFlat = require('../../lib/extend').extendFlat;
var Plots = require('../plots');
var Axes = require('../cartesian/axes');
var dragElement = require('../../components/dragelement');
var Titles = require('../../components/titles');
var prepSelect = require('../cartesian/select');
var constants = require('../cartesian/constants');
var fx = require('../cartesian/graph_interact');
function Ternary(options, fullLayout) {
this.id = options.id;
this.graphDiv = options.graphDiv;
this.init(fullLayout);
this.makeFramework();
}
module.exports = Ternary;
var proto = Ternary.prototype;
proto.init = function(fullLayout) {
this.container = fullLayout._ternarylayer;
this.defs = fullLayout._defs;
this.layoutId = fullLayout._uid;
this.traceHash = {};
};
proto.plot = function(ternaryCalcData, fullLayout) {
var _this = this,
ternaryLayout = fullLayout[_this.id],
graphSize = fullLayout._size;
_this.adjustLayout(ternaryLayout, graphSize);
Plots.generalUpdatePerTraceModule(_this, ternaryCalcData, ternaryLayout);
_this.layers.plotbg.select('path').call(Color.fill, ternaryLayout.bgcolor);
};
proto.makeFramework = function() {
var _this = this;
var defGroup = _this.defs.selectAll('g.clips')
.data([0]);
defGroup.enter().append('g')
.classed('clips', true);
// clippath for this ternary subplot
var clipId = 'clip' + _this.layoutId + _this.id;
_this.clipDef = defGroup.selectAll('#' + clipId)
.data([0]);
_this.clipDef.enter().append('clipPath').attr('id', clipId)
.append('path').attr('d', 'M0,0Z');
// container for everything in this ternary subplot
_this.plotContainer = _this.container.selectAll('g.' + _this.id)
.data([0]);
_this.plotContainer.enter().append('g')
.classed(_this.id, true);
_this.layers = {};
// inside that container, we have one container for the data, and
// one each for the three axes around it.
var plotLayers = [
'draglayer',
'plotbg',
'backplot',
'grids',
'frontplot',
'zoom',
'aaxis', 'baxis', 'caxis', 'axlines'
];
var toplevel = _this.plotContainer.selectAll('g.toplevel')
.data(plotLayers);
toplevel.enter().append('g')
.attr('class', function(d) { return 'toplevel ' + d; })
.each(function(d) {
var s = d3.select(this);
_this.layers[d] = s;
// containers for different trace types.
// NOTE - this is different from cartesian, where all traces
// are in front of grids. Here I'm putting maps behind the grids
// so the grids will always be visible if they're requested.
// Perhaps we want that for cartesian too?
if(d === 'frontplot') s.append('g').classed('scatterlayer', true);
else if(d === 'backplot') s.append('g').classed('maplayer', true);
else if(d === 'plotbg') s.append('path').attr('d', 'M0,0Z');
else if(d === 'axlines') {
s.selectAll('path').data(['aline', 'bline', 'cline'])
.enter().append('path').each(function(d) {
d3.select(this).classed(d, true);
});
}
});
var grids = _this.plotContainer.select('.grids').selectAll('g.grid')
.data(['agrid', 'bgrid', 'cgrid']);
grids.enter().append('g')
.attr('class', function(d) { return 'grid ' + d; })
.each(function(d) { _this.layers[d] = d3.select(this); });
_this.plotContainer.selectAll('.backplot,.frontplot,.grids')
.call(Drawing.setClipUrl, clipId);
if(!_this.graphDiv._context.staticPlot) {
_this.initInteractions();
}
};
var w_over_h = Math.sqrt(4 / 3);
proto.adjustLayout = function(ternaryLayout, graphSize) {
var _this = this,
domain = ternaryLayout.domain,
xDomainCenter = (domain.x[0] + domain.x[1]) / 2,
yDomainCenter = (domain.y[0] + domain.y[1]) / 2,
xDomain = domain.x[1] - domain.x[0],
yDomain = domain.y[1] - domain.y[0],
wmax = xDomain * graphSize.w,
hmax = yDomain * graphSize.h,
sum = ternaryLayout.sum,
amin = ternaryLayout.aaxis.min,
bmin = ternaryLayout.baxis.min,
cmin = ternaryLayout.caxis.min;
var x0, y0, w, h, xDomainFinal, yDomainFinal;
if(wmax > w_over_h * hmax) {
h = hmax;
w = h * w_over_h;
}
else {
w = wmax;
h = w / w_over_h;
}
xDomainFinal = xDomain * w / wmax;
yDomainFinal = yDomain * h / hmax;
x0 = graphSize.l + graphSize.w * xDomainCenter - w / 2;
y0 = graphSize.t + graphSize.h * (1 - yDomainCenter) - h / 2;
_this.x0 = x0;
_this.y0 = y0;
_this.w = w;
_this.h = h;
_this.sum = sum;
// set up the x and y axis objects we'll use to lay out the points
_this.xaxis = {
type: 'linear',
range: [amin + 2 * cmin - sum, sum - amin - 2 * bmin],
domain: [
xDomainCenter - xDomainFinal / 2,
xDomainCenter + xDomainFinal / 2
],
_id: 'x'
};
setConvert(_this.xaxis, _this.graphDiv._fullLayout);
_this.xaxis.setScale();
_this.yaxis = {
type: 'linear',
range: [amin, sum - bmin - cmin],
domain: [
yDomainCenter - yDomainFinal / 2,
yDomainCenter + yDomainFinal / 2
],
_id: 'y'
};
setConvert(_this.yaxis, _this.graphDiv._fullLayout);
_this.yaxis.setScale();
// set up the modified axes for tick drawing
var yDomain0 = _this.yaxis.domain[0];
// aaxis goes up the left side. Set it up as a y axis, but with
// fictitious angles and domain, but then rotate and translate
// it into place at the end
var aaxis = _this.aaxis = extendFlat({}, ternaryLayout.aaxis, {
visible: true,
range: [amin, sum - bmin - cmin],
side: 'left',
_counterangle: 30,
// tickangle = 'auto' means 0 anyway for a y axis, need to coerce to 0 here
// so we can shift by 30.
tickangle: (+ternaryLayout.aaxis.tickangle || 0) - 30,
domain: [yDomain0, yDomain0 + yDomainFinal * w_over_h],
_axislayer: _this.layers.aaxis,
_gridlayer: _this.layers.agrid,
_pos: 0, // _this.xaxis.domain[0] * graphSize.w,
_id: 'y',
_length: w,
_gridpath: 'M0,0l' + h + ',-' + (w / 2)
});
setConvert(aaxis, _this.graphDiv._fullLayout);
aaxis.setScale();
// baxis goes across the bottom (backward). We can set it up as an x axis
// without any enclosing transformation.
var baxis = _this.baxis = extendFlat({}, ternaryLayout.baxis, {
visible: true,
range: [sum - amin - cmin, bmin],
side: 'bottom',
_counterangle: 30,
domain: _this.xaxis.domain,
_axislayer: _this.layers.baxis,
_gridlayer: _this.layers.bgrid,
_counteraxis: _this.aaxis,
_pos: 0, // (1 - yDomain0) * graphSize.h,
_id: 'x',
_length: w,
_gridpath: 'M0,0l-' + (w / 2) + ',-' + h
});
setConvert(baxis, _this.graphDiv._fullLayout);
baxis.setScale();
aaxis._counteraxis = baxis;
// caxis goes down the right side. Set it up as a y axis, with
// post-transformation similar to aaxis
var caxis = _this.caxis = extendFlat({}, ternaryLayout.caxis, {
visible: true,
range: [sum - amin - bmin, cmin],
side: 'right',
_counterangle: 30,
tickangle: (+ternaryLayout.caxis.tickangle || 0) + 30,
domain: [yDomain0, yDomain0 + yDomainFinal * w_over_h],
_axislayer: _this.layers.caxis,
_gridlayer: _this.layers.cgrid,
_counteraxis: _this.baxis,
_pos: 0, // _this.xaxis.domain[1] * graphSize.w,
_id: 'y',
_length: w,
_gridpath: 'M0,0l-' + h + ',' + (w / 2)
});
setConvert(caxis, _this.graphDiv._fullLayout);
caxis.setScale();
var triangleClip = 'M' + x0 + ',' + (y0 + h) + 'h' + w + 'l-' + (w / 2) + ',-' + h + 'Z';
_this.clipDef.select('path').attr('d', triangleClip);
_this.layers.plotbg.select('path').attr('d', triangleClip);
var plotTransform = 'translate(' + x0 + ',' + y0 + ')';
_this.plotContainer.selectAll('.scatterlayer,.maplayer,.zoom')
.attr('transform', plotTransform);
// TODO: shift axes to accommodate linewidth*sin(30) tick mark angle
var bTransform = 'translate(' + x0 + ',' + (y0 + h) + ')';
_this.layers.baxis.attr('transform', bTransform);
_this.layers.bgrid.attr('transform', bTransform);
var aTransform = 'translate(' + (x0 + w / 2) + ',' + y0 + ')rotate(30)';
_this.layers.aaxis.attr('transform', aTransform);
_this.layers.agrid.attr('transform', aTransform);
var cTransform = 'translate(' + (x0 + w / 2) + ',' + y0 + ')rotate(-30)';
_this.layers.caxis.attr('transform', cTransform);
_this.layers.cgrid.attr('transform', cTransform);
_this.drawAxes(true);
// remove crispEdges - all the off-square angles in ternary plots
// make these counterproductive.
_this.plotContainer.selectAll('.crisp').classed('crisp', false);
var axlines = _this.layers.axlines;
axlines.select('.aline')
.attr('d', aaxis.showline ?
'M' + x0 + ',' + (y0 + h) + 'l' + (w / 2) + ',-' + h : 'M0,0')
.call(Color.stroke, aaxis.linecolor || '#000')
.style('stroke-width', (aaxis.linewidth || 0) + 'px');
axlines.select('.bline')
.attr('d', baxis.showline ?
'M' + x0 + ',' + (y0 + h) + 'h' + w : 'M0,0')
.call(Color.stroke, baxis.linecolor || '#000')
.style('stroke-width', (baxis.linewidth || 0) + 'px');
axlines.select('.cline')
.attr('d', caxis.showline ?
'M' + (x0 + w / 2) + ',' + y0 + 'l' + (w / 2) + ',' + h : 'M0,0')
.call(Color.stroke, caxis.linecolor || '#000')
.style('stroke-width', (caxis.linewidth || 0) + 'px');
};
proto.drawAxes = function(doTitles) {
var _this = this,
gd = _this.graphDiv,
titlesuffix = _this.id.substr(7) + 'title',
aaxis = _this.aaxis,
baxis = _this.baxis,
caxis = _this.caxis;
// 3rd arg true below skips titles, so we can configure them
// correctly later on.
Axes.doTicks(gd, aaxis, true);
Axes.doTicks(gd, baxis, true);
Axes.doTicks(gd, caxis, true);
if(doTitles) {
var apad = Math.max(aaxis.showticklabels ? aaxis.tickfont.size / 2 : 0,
(caxis.showticklabels ? caxis.tickfont.size * 0.75 : 0) +
(caxis.ticks === 'outside' ? caxis.ticklen * 0.87 : 0));
Titles.draw(gd, 'a' + titlesuffix, {
propContainer: aaxis,
propName: _this.id + '.aaxis.title',
dfltName: 'Component A',
attributes: {
x: _this.x0 + _this.w / 2,
y: _this.y0 - aaxis.titlefont.size / 3 - apad,
'text-anchor': 'middle'
}
});
var bpad = (baxis.showticklabels ? baxis.tickfont.size : 0) +
(baxis.ticks === 'outside' ? baxis.ticklen : 0) + 3;
Titles.draw(gd, 'b' + titlesuffix, {
propContainer: baxis,
propName: _this.id + '.baxis.title',
dfltName: 'Component B',
attributes: {
x: _this.x0 - bpad,
y: _this.y0 + _this.h + baxis.titlefont.size * 0.83 + bpad,
'text-anchor': 'middle'
}
});
Titles.draw(gd, 'c' + titlesuffix, {
propContainer: caxis,
propName: _this.id + '.caxis.title',
dfltName: 'Component C',
attributes: {
x: _this.x0 + _this.w + bpad,
y: _this.y0 + _this.h + caxis.titlefont.size * 0.83 + bpad,
'text-anchor': 'middle'
}
});
}
};
// hard coded paths for zoom corners
// uses the same sizing as cartesian, length is MINZOOM/2, width is 3px
var CLEN = constants.MINZOOM / 2 + 0.87;
var BLPATH = 'm-0.87,.5h' + CLEN + 'v3h-' + (CLEN + 5.2) +
'l' + (CLEN / 2 + 2.6) + ',-' + (CLEN * 0.87 + 4.5) +
'l2.6,1.5l-' + (CLEN / 2) + ',' + (CLEN * 0.87) + 'Z';
var BRPATH = 'm0.87,.5h-' + CLEN + 'v3h' + (CLEN + 5.2) +
'l-' + (CLEN / 2 + 2.6) + ',-' + (CLEN * 0.87 + 4.5) +
'l-2.6,1.5l' + (CLEN / 2) + ',' + (CLEN * 0.87) + 'Z';
var TOPPATH = 'm0,1l' + (CLEN / 2) + ',' + (CLEN * 0.87) +
'l2.6,-1.5l-' + (CLEN / 2 + 2.6) + ',-' + (CLEN * 0.87 + 4.5) +
'l-' + (CLEN / 2 + 2.6) + ',' + (CLEN * 0.87 + 4.5) +
'l2.6,1.5l' + (CLEN / 2) + ',-' + (CLEN * 0.87) + 'Z';
var STARTMARKER = 'm0.5,0.5h5v-2h-5v-5h-2v5h-5v2h5v5h2Z';
// I guess this could be shared with cartesian... but for now it's separate.
var SHOWZOOMOUTTIP = true;
proto.initInteractions = function() {
var _this = this,
dragger = _this.layers.plotbg.select('path').node(),
gd = _this.graphDiv,
zoomContainer = _this.layers.zoom;
// use plotbg for the main interactions
var dragOptions = {
element: dragger,
gd: gd,
plotinfo: {plot: zoomContainer},
doubleclick: doubleClick,
subplot: _this.id,
prepFn: function(e, startX, startY) {
// these aren't available yet when initInteractions
// is called
dragOptions.xaxes = [_this.xaxis];
dragOptions.yaxes = [_this.yaxis];
var dragModeNow = gd._fullLayout.dragmode;
if(e.shiftKey) {
if(dragModeNow === 'pan') dragModeNow = 'zoom';
else dragModeNow = 'pan';
}
if(dragModeNow === 'lasso') dragOptions.minDrag = 1;
else dragOptions.minDrag = undefined;
if(dragModeNow === 'zoom') {
dragOptions.moveFn = zoomMove;
dragOptions.doneFn = zoomDone;
zoomPrep(e, startX, startY);
}
else if(dragModeNow === 'pan') {
dragOptions.moveFn = plotDrag;
dragOptions.doneFn = dragDone;
panPrep();
clearSelect();
}
else if(dragModeNow === 'select' || dragModeNow === 'lasso') {
prepSelect(e, startX, startY, dragOptions, dragModeNow);
}
}
};
var x0, y0, mins0, span0, mins, lum, path0, dimmed, zb, corners;
function zoomPrep(e, startX, startY) {
var dragBBox = dragger.getBoundingClientRect();
x0 = startX - dragBBox.left;
y0 = startY - dragBBox.top;
mins0 = {
a: _this.aaxis.range[0],
b: _this.baxis.range[1],
c: _this.caxis.range[1]
};
mins = mins0;
span0 = _this.aaxis.range[1] - mins0.a;
lum = tinycolor(_this.graphDiv._fullLayout[_this.id].bgcolor).getLuminance();
path0 = 'M0,' + _this.h + 'L' + (_this.w / 2) + ', 0L' + _this.w + ',' + _this.h + 'Z';
dimmed = false;
zb = zoomContainer.append('path')
.attr('class', 'zoombox')
.style({
'fill': lum > 0.2 ? 'rgba(0,0,0,0)' : 'rgba(255,255,255,0)',
'stroke-width': 0
})
.attr('d', path0);
corners = zoomContainer.append('path')
.attr('class', 'zoombox-corners')
.style({
fill: Color.background,
stroke: Color.defaultLine,
'stroke-width': 1,
opacity: 0
})
.attr('d', 'M0,0Z');
clearSelect();
}
function getAFrac(x, y) { return 1 - (y / _this.h); }
function getBFrac(x, y) { return 1 - ((x + (_this.h - y) / Math.sqrt(3)) / _this.w); }
function getCFrac(x, y) { return ((x - (_this.h - y) / Math.sqrt(3)) / _this.w); }
function zoomMove(dx0, dy0) {
var x1 = x0 + dx0,
y1 = y0 + dy0,
afrac = Math.max(0, Math.min(1, getAFrac(x0, y0), getAFrac(x1, y1))),
bfrac = Math.max(0, Math.min(1, getBFrac(x0, y0), getBFrac(x1, y1))),
cfrac = Math.max(0, Math.min(1, getCFrac(x0, y0), getCFrac(x1, y1))),
xLeft = ((afrac / 2) + cfrac) * _this.w,
xRight = (1 - (afrac / 2) - bfrac) * _this.w,
xCenter = (xLeft + xRight) / 2,
xSpan = xRight - xLeft,
yBottom = (1 - afrac) * _this.h,
yTop = yBottom - xSpan / w_over_h;
if(xSpan < constants.MINZOOM) {
mins = mins0;
zb.attr('d', path0);
corners.attr('d', 'M0,0Z');
}
else {
mins = {
a: mins0.a + afrac * span0,
b: mins0.b + bfrac * span0,
c: mins0.c + cfrac * span0
};
zb.attr('d', path0 + 'M' + xLeft + ',' + yBottom +
'H' + xRight + 'L' + xCenter + ',' + yTop +
'L' + xLeft + ',' + yBottom + 'Z');
corners.attr('d', 'M' + x0 + ',' + y0 + STARTMARKER +
'M' + xLeft + ',' + yBottom + BLPATH +
'M' + xRight + ',' + yBottom + BRPATH +
'M' + xCenter + ',' + yTop + TOPPATH);
}
if(!dimmed) {
zb.transition()
.style('fill', lum > 0.2 ? 'rgba(0,0,0,0.4)' :
'rgba(255,255,255,0.3)')
.duration(200);
corners.transition()
.style('opacity', 1)
.duration(200);
dimmed = true;
}
}
function zoomDone(dragged, numClicks) {
if(mins === mins0) {
if(numClicks === 2) doubleClick();
return removeZoombox(gd);
}
removeZoombox(gd);
var attrs = {};
attrs[_this.id + '.aaxis.min'] = mins.a;
attrs[_this.id + '.baxis.min'] = mins.b;
attrs[_this.id + '.caxis.min'] = mins.c;
Plotly.relayout(gd, attrs);
if(SHOWZOOMOUTTIP && gd.data && gd._context.showTips) {
Lib.notifier('Double-click to<br>zoom back out', 'long');
SHOWZOOMOUTTIP = false;
}
}
function panPrep() {
mins0 = {
a: _this.aaxis.range[0],
b: _this.baxis.range[1],
c: _this.caxis.range[1]
};
mins = mins0;
}
function plotDrag(dx, dy) {
var dxScaled = dx / _this.xaxis._m,
dyScaled = dy / _this.yaxis._m;
mins = {
a: mins0.a - dyScaled,
b: mins0.b + (dxScaled + dyScaled) / 2,
c: mins0.c - (dxScaled - dyScaled) / 2
};
var minsorted = [mins.a, mins.b, mins.c].sort(),
minindices = {
a: minsorted.indexOf(mins.a),
b: minsorted.indexOf(mins.b),
c: minsorted.indexOf(mins.c)
};
if(minsorted[0] < 0) {
if(minsorted[1] + minsorted[0] / 2 < 0) {
minsorted[2] += minsorted[0] + minsorted[1];
minsorted[0] = minsorted[1] = 0;
}
else {
minsorted[2] += minsorted[0] / 2;
minsorted[1] += minsorted[0] / 2;
minsorted[0] = 0;
}
mins = {
a: minsorted[minindices.a],
b: minsorted[minindices.b],
c: minsorted[minindices.c]
};
dy = (mins0.a - mins.a) * _this.yaxis._m;
dx = (mins0.c - mins.c - mins0.b + mins.b) * _this.xaxis._m;
}
// move the data (translate, don't redraw)
var plotTransform = 'translate(' + (_this.x0 + dx) + ',' + (_this.y0 + dy) + ')';
_this.plotContainer.selectAll('.scatterlayer,.maplayer')
.attr('transform', plotTransform);
// move the ticks
_this.aaxis.range = [mins.a, _this.sum - mins.b - mins.c];
_this.baxis.range = [_this.sum - mins.a - mins.c, mins.b];
_this.caxis.range = [_this.sum - mins.a - mins.b, mins.c];
_this.drawAxes(false);
_this.plotContainer.selectAll('.crisp').classed('crisp', false);
}
function dragDone(dragged, numClicks) {
if(dragged) {
var attrs = {};
attrs[_this.id + '.aaxis.min'] = mins.a;
attrs[_this.id + '.baxis.min'] = mins.b;
attrs[_this.id + '.caxis.min'] = mins.c;
Plotly.relayout(gd, attrs);
}
else if(numClicks === 2) doubleClick();
}
function clearSelect() {
// until we get around to persistent selections, remove the outline
// here. The selection itself will be removed when the plot redraws
// at the end.
_this.plotContainer.selectAll('.select-outline').remove();
}
function doubleClick() {
var attrs = {};
attrs[_this.id + '.aaxis.min'] = 0;
attrs[_this.id + '.baxis.min'] = 0;
attrs[_this.id + '.caxis.min'] = 0;
gd.emit('plotly_doubleclick', null);
Plotly.relayout(gd, attrs);
}
// finally, set up hover and click
// these event handlers must already be set before dragElement.init
// so it can stash them and override them.
dragger.onmousemove = function(evt) {
fx.hover(gd, evt, _this.id);
gd._fullLayout._lasthover = dragger;
gd._fullLayout._hoversubplot = _this.id;
};
dragger.onmouseout = function(evt) {
if(gd._dragging) return;
dragElement.unhover(gd, evt);
};
dragger.onclick = function(evt) {
fx.click(gd, evt);
};
dragElement.init(dragOptions);
};
function removeZoombox(gd) {
d3.select(gd)
.selectAll('.zoombox,.js-zoombox-backdrop,.js-zoombox-menu,.zoombox-corners')
.remove();
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| cloneplot.js | 10% | (7 / 70) | 0% | (0 / 51) | 0% | (0 / 3) | 10.77% | (7 / 65) | |
| download.js | 22.73% | (5 / 22) | 0% | (0 / 13) | 0% | (0 / 5) | 22.73% | (5 / 22) | |
| filesaver.js | 8% | (2 / 25) | 0% | (0 / 14) | 0% | (0 / 2) | 8% | (2 / 25) | |
| helpers.js | 18.18% | (2 / 11) | 0% | (0 / 18) | 0% | (0 / 4) | 25% | (2 / 8) | |
| svgtoimg.js | 7.69% | (4 / 52) | 0% | (0 / 29) | 0% | (0 / 4) | 7.69% | (4 / 52) | |
| toimage.js | 34.48% | (10 / 29) | 0% | (0 / 2) | 0% | (0 / 5) | 35.71% | (10 / 28) | |
| tosvg.js | 14.63% | (6 / 41) | 0% | (0 / 20) | 0% | (0 / 2) | 15.38% | (6 / 39) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var Plots = require('../plots/plots');
var extendFlat = Lib.extendFlat;
var extendDeep = Lib.extendDeep;
// Put default plotTile layouts here
function cloneLayoutOverride(tileClass) {
var override;
switch(tileClass) {
case 'themes__thumb':
override = {
autosize: true,
width: 150,
height: 150,
title: '',
showlegend: false,
margin: {l: 5, r: 5, t: 5, b: 5, pad: 0},
annotations: []
};
break;
case 'thumbnail':
override = {
title: '',
hidesources: true,
showlegend: false,
borderwidth: 0,
bordercolor: '',
margin: {l: 1, r: 1, t: 1, b: 1, pad: 0},
annotations: []
};
break;
default:
override = {};
}
return override;
}
function keyIsAxis(keyName) {
var types = ['xaxis', 'yaxis', 'zaxis'];
return (types.indexOf(keyName.slice(0, 5)) > -1);
}
module.exports = function clonePlot(graphObj, options) {
// Polar plot compatibility
if(graphObj.framework && graphObj.framework.isPolar) {
graphObj = graphObj.framework.getConfig();
}
var i;
var oldData = graphObj.data;
var oldLayout = graphObj.layout;
var newData = extendDeep([], oldData);
var newLayout = extendDeep({}, oldLayout, cloneLayoutOverride(options.tileClass));
var context = graphObj._context || {};
if(options.width) newLayout.width = options.width;
if(options.height) newLayout.height = options.height;
if(options.tileClass === 'thumbnail' || options.tileClass === 'themes__thumb') {
// kill annotations
newLayout.annotations = [];
var keys = Object.keys(newLayout);
for(i = 0; i < keys.length; i++) {
if(keyIsAxis(keys[i])) {
newLayout[keys[i]].title = '';
}
}
// kill colorbar and pie labels
for(i = 0; i < newData.length; i++) {
var trace = newData[i];
trace.showscale = false;
if(trace.marker) trace.marker.showscale = false;
if(trace.type === 'pie') trace.textposition = 'none';
}
}
if(Array.isArray(options.annotations)) {
for(i = 0; i < options.annotations.length; i++) {
newLayout.annotations.push(options.annotations[i]);
}
}
var sceneIds = Plots.getSubplotIds(newLayout, 'gl3d');
if(sceneIds.length) {
var axesImageOverride = {};
if(options.tileClass === 'thumbnail') {
axesImageOverride = {
title: '',
showaxeslabels: false,
showticklabels: false,
linetickenable: false
};
}
for(i = 0; i < sceneIds.length; i++) {
var scene = newLayout[sceneIds[i]];
if(!scene.xaxis) {
scene.xaxis = {};
}
if(!scene.yaxis) {
scene.yaxis = {};
}
if(!scene.zaxis) {
scene.zaxis = {};
}
extendFlat(scene.xaxis, axesImageOverride);
extendFlat(scene.yaxis, axesImageOverride);
extendFlat(scene.zaxis, axesImageOverride);
// TODO what does this do?
scene._scene = null;
}
}
var gd = document.createElement('div');
if(options.tileClass) gd.className = options.tileClass;
var plotTile = {
gd: gd,
td: gd, // for external (image server) compatibility
layout: newLayout,
data: newData,
config: {
staticPlot: (options.staticPlot === undefined) ?
true :
options.staticPlot,
plotGlPixelRatio: (options.plotGlPixelRatio === undefined) ?
2 :
options.plotGlPixelRatio,
displaylogo: options.displaylogo || false,
showLink: options.showLink || false,
showTips: options.showTips || false,
mapboxAccessToken: context.mapboxAccessToken
}
};
if(options.setBackground !== 'transparent') {
plotTile.config.setBackground = options.setBackground || 'opaque';
}
// attaching the default Layout the gd, so you can grab it later
plotTile.gd.defaultLayout = cloneLayoutOverride(options.tileClass);
return plotTile;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var toImage = require('../plot_api/to_image');
var Lib = require('../lib'); // for isIE
var fileSaver = require('./filesaver');
/**
* @param {object} gd figure Object
* @param {object} opts option object
* @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
* @param opts.width width of snapshot in px
* @param opts.height height of snapshot in px
* @param opts.filename name of file excluding extension
*/
function downloadImage(gd, opts) {
// check for undefined opts
opts = opts || {};
// default to png
opts.format = opts.format || 'png';
return new Promise(function(resolve, reject) {
if(gd._snapshotInProgress) {
reject(new Error('Snapshotting already in progress.'));
}
// see comments within svgtoimg for additional
// discussion of problems with IE
// can now draw to canvas, but CORS tainted canvas
// does not allow toDataURL
// svg format will work though
if(Lib.isIE() && opts.format !== 'svg') {
reject(new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.'));
}
gd._snapshotInProgress = true;
var promise = toImage(gd, opts);
var filename = opts.filename || gd.fn || 'newplot';
filename += '.' + opts.format;
promise.then(function(result) {
gd._snapshotInProgress = false;
return fileSaver(result, filename);
}).then(function(name) {
resolve(name);
}).catch(function(err) {
gd._snapshotInProgress = false;
reject(err);
});
});
}
module.exports = downloadImage;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ /* * substantial portions of this code from FileSaver.js * https://github.com/eligrey/FileSaver.js * License: https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md * FileSaver.js * A saveAs() FileSaver implementation. * 1.1.20160328 * * By Eli Grey, http://eligrey.com * License: MIT * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md */ 'use strict'; var fileSaver = function(url, name) { var saveLink = document.createElement('a'); var canUseSaveLink = 'download' in saveLink; var isSafari = /Version\/[\d\.]+.*Safari/.test(navigator.userAgent); var promise = new Promise(function(resolve, reject) { // IE <10 is explicitly unsupported if(typeof navigator !== 'undefined' && /MSIE [1-9]\./.test(navigator.userAgent)) { reject(new Error('IE < 10 unsupported')); } // First try a.download, then web filesystem, then object URLs if(isSafari) { // Safari doesn't allow downloading of blob urls document.location.href = 'data:application/octet-stream' + url.slice(url.search(/[,;]/)); resolve(name); } if(!name) { name = 'download'; } if(canUseSaveLink) { saveLink.href = url; saveLink.download = name; document.body.appendChild(saveLink); saveLink.click(); document.body.removeChild(saveLink); resolve(name); } // IE 10+ (native saveAs) if(typeof navigator !== 'undefined' && navigator.msSaveBlob) { navigator.msSaveBlob(new Blob([url]), name); resolve(name); } reject(new Error('download error')); }); return promise; }; module.exports = fileSaver; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; exports.getDelay = function(fullLayout) { // polar clears fullLayout._has for some reason if(!fullLayout._has) return 0; // maybe we should add a 'gl' (and 'svg') layoutCategory ?? return (fullLayout._has('gl3d') || fullLayout._has('gl2d')) ? 500 : 0; }; exports.getRedrawFunc = function(gd) { // do not work if polar is present if((gd.data && gd.data[0] && gd.data[0].r)) return; return function() { (gd.calcdata || []).forEach(function(d) { if(d[0] && d[0].t && d[0].t.cb) d[0].t.cb(); }); }; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var EventEmitter = require('events').EventEmitter;
function svgToImg(opts) {
var ev = opts.emitter || new EventEmitter();
var promise = new Promise(function(resolve, reject) {
var Image = window.Image;
var svg = opts.svg;
var format = opts.format || 'png';
// IE is very strict, so we will need to clean
// svg with the following regex
// yes this is messy, but do not know a better way
// Even with this IE will not work due to tainted canvas
// see https://github.com/kangax/fabric.js/issues/1957
// http://stackoverflow.com/questions/18112047/canvas-todataurl-working-in-all-browsers-except-ie10
// Leave here just in case the CORS/tainted IE issue gets resolved
if(Lib.isIE()) {
// replace double quote with single quote
svg = svg.replace(/"/gi, '\'');
// url in svg are single quoted
// since we changed double to single
// we'll need to change these to double-quoted
svg = svg.replace(/(\('#)(.*)('\))/gi, '(\"$2\")');
// font names with spaces will be escaped single-quoted
// we'll need to change these to double-quoted
svg = svg.replace(/(\\')/gi, '\"');
// IE only support svg
if(format !== 'svg') {
var ieSvgError = new Error('Sorry IE does not support downloading from canvas. Try {format:\'svg\'} instead.');
reject(ieSvgError);
// eventually remove the ev
// in favor of promises
if(!opts.promise) {
return ev.emit('error', ieSvgError);
} else {
return promise;
}
}
}
var canvas = opts.canvas;
var ctx = canvas.getContext('2d');
var img = new Image();
// for Safari support, eliminate createObjectURL
// this decision could cause problems if content
// is not restricted to svg
var url = 'data:image/svg+xml,' + encodeURIComponent(svg);
canvas.height = opts.height || 150;
canvas.width = opts.width || 300;
img.onload = function() {
var imgData;
// don't need to draw to canvas if svg
// save some time and also avoid failure on IE
if(format !== 'svg') {
ctx.drawImage(img, 0, 0);
}
switch(format) {
case 'jpeg':
imgData = canvas.toDataURL('image/jpeg');
break;
case 'png':
imgData = canvas.toDataURL('image/png');
break;
case 'webp':
imgData = canvas.toDataURL('image/webp');
break;
case 'svg':
imgData = url;
break;
default:
reject(new Error('Image format is not jpeg, png or svg'));
// eventually remove the ev
// in favor of promises
if(!opts.promise) {
return ev.emit('error', 'Image format is not jpeg, png or svg');
}
}
resolve(imgData);
// eventually remove the ev
// in favor of promises
if(!opts.promise) {
ev.emit('success', imgData);
}
};
img.onerror = function(err) {
reject(err);
// eventually remove the ev
// in favor of promises
if(!opts.promise) {
return ev.emit('error', err);
}
};
img.src = url;
});
// temporary for backward compatibility
// move to only Promise in 2.0.0
// and eliminate the EventEmitter
if(opts.promise) {
return promise;
}
return ev;
}
module.exports = svgToImg;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var EventEmitter = require('events').EventEmitter;
var Plotly = require('../plotly');
var Lib = require('../lib');
var helpers = require('./helpers');
var clonePlot = require('./cloneplot');
var toSVG = require('./tosvg');
var svgToImg = require('./svgtoimg');
/**
* @param {object} gd figure Object
* @param {object} opts option object
* @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
*/
function toImage(gd, opts) {
// first clone the GD so we can operate in a clean environment
var ev = new EventEmitter();
var clone = clonePlot(gd, {format: 'png'});
var clonedGd = clone.gd;
// put the cloned div somewhere off screen before attaching to DOM
clonedGd.style.position = 'absolute';
clonedGd.style.left = '-5000px';
document.body.appendChild(clonedGd);
function wait() {
var delay = helpers.getDelay(clonedGd._fullLayout);
setTimeout(function() {
var svg = toSVG(clonedGd);
var canvas = document.createElement('canvas');
canvas.id = Lib.randstr();
ev = svgToImg({
format: opts.format,
width: clonedGd._fullLayout.width,
height: clonedGd._fullLayout.height,
canvas: canvas,
emitter: ev,
svg: svg
});
ev.clean = function() {
if(clonedGd) document.body.removeChild(clonedGd);
};
}, delay);
}
var redrawFunc = helpers.getRedrawFunc(clonedGd);
Plotly.plot(clonedGd, clone.data, clone.layout, clone.config)
.then(redrawFunc)
.then(wait)
.catch(function(err) {
ev.emit('error', err);
});
return ev;
}
module.exports = toImage;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var svgTextUtils = require('../lib/svg_text_utils');
var Drawing = require('../components/drawing');
var Color = require('../components/color');
var xmlnsNamespaces = require('../constants/xmlns_namespaces');
module.exports = function toSVG(gd, format) {
var fullLayout = gd._fullLayout,
svg = fullLayout._paper,
toppaper = fullLayout._toppaper,
i;
// make background color a rect in the svg, then revert after scraping
// all other alterations have been dealt with by properly preparing the svg
// in the first place... like setting cursors with css classes so we don't
// have to remove them, and providing the right namespaces in the svg to
// begin with
svg.insert('rect', ':first-child')
.call(Drawing.setRect, 0, 0, fullLayout.width, fullLayout.height)
.call(Color.fill, fullLayout.paper_bgcolor);
// subplot-specific to-SVG methods
// which notably add the contents of the gl-container
// into the main svg node
var basePlotModules = fullLayout._basePlotModules || [];
for(i = 0; i < basePlotModules.length; i++) {
var _module = basePlotModules[i];
if(_module.toSVG) _module.toSVG(gd);
}
// add top items above them assumes everything in toppaper is either
// a group or a defs, and if it's empty (like hoverlayer) we can ignore it.
if(toppaper) {
var nodes = toppaper.node().childNodes;
// make copy of nodes as childNodes prop gets mutated in loop below
var topGroups = Array.prototype.slice.call(nodes);
for(i = 0; i < topGroups.length; i++) {
var topGroup = topGroups[i];
if(topGroup.childNodes.length) svg.node().appendChild(topGroup);
}
}
// remove draglayer for Adobe Illustrator compatibility
if(fullLayout._draggers) {
fullLayout._draggers.remove();
}
// in case the svg element had an explicit background color, remove this
// we want the rect to get the color so it's the right size; svg bg will
// fill whatever container it's displayed in regardless of plot size.
svg.node().style.background = '';
svg.selectAll('text')
.attr('data-unformatted', null)
.each(function() {
var txt = d3.select(this);
// hidden text is pre-formatting mathjax,
// the browser ignores it but it can still confuse batik
if(txt.style('visibility') === 'hidden') {
txt.remove();
return;
}
else {
// force other visibility value to export as visible
// to not potentially confuse non-browser SVG implementations
txt.style('visibility', 'visible');
}
// Font family styles break things because of quotation marks,
// so we must remove them *after* the SVG DOM has been serialized
// to a string (browsers convert singles back)
var ff = txt.style('font-family');
if(ff && ff.indexOf('"') !== -1) {
txt.style('font-family', ff.replace(/"/g, 'TOBESTRIPPED'));
}
});
if(format === 'pdf' || format === 'eps') {
// these formats make the extra line MathJax adds around symbols look super thick in some cases
// it looks better if this is removed entirely.
svg.selectAll('#MathJax_SVG_glyphs path')
.attr('stroke-width', 0);
}
// fix for IE namespacing quirk?
// http://stackoverflow.com/questions/19610089/unwanted-namespaces-on-svg-markup-when-using-xmlserializer-in-javascript-with-ie
svg.node().setAttributeNS(xmlnsNamespaces.xmlns, 'xmlns', xmlnsNamespaces.svg);
svg.node().setAttributeNS(xmlnsNamespaces.xmlns, 'xmlns:xlink', xmlnsNamespaces.xlink);
var s = new window.XMLSerializer().serializeToString(svg.node());
s = svgTextUtils.html_entity_decode(s);
s = svgTextUtils.xml_entity_encode(s);
// Fix quotations around font strings
s = s.replace(/("TOBESTRIPPED)|(TOBESTRIPPED")/g, '\'');
return s;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| arrays_to_calcdata.js | 16.67% | (2 / 12) | 0% | (0 / 4) | 0% | (0 / 1) | 16.67% | (2 / 12) | |
| attributes.js | 5.88% | (1 / 17) | 100% | (0 / 0) | 100% | (0 / 0) | 5.88% | (1 / 17) | |
| hover.js | 6.25% | (4 / 64) | 0% | (0 / 32) | 0% | (0 / 10) | 7.55% | (4 / 53) | |
| index.js | 11.11% | (2 / 18) | 100% | (0 / 0) | 100% | (0 / 0) | 11.11% | (2 / 18) | |
| set_positions.js | 9.32% | (22 / 236) | 0% | (0 / 103) | 0% | (0 / 17) | 10.05% | (22 / 219) | |
| sieve.js | 23.33% | (7 / 30) | 0% | (0 / 12) | 0% | (0 / 4) | 24.14% | (7 / 29) | |
| style_defaults.js | 36.36% | (4 / 11) | 0% | (0 / 4) | 0% | (0 / 1) | 36.36% | (4 / 11) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var mergeArray = require('../../lib').mergeArray;
// arrayOk attributes, merge them into calcdata array
module.exports = function arraysToCalcdata(cd, trace) {
mergeArray(trace.text, cd, 'tx');
mergeArray(trace.hovertext, cd, 'htx');
var marker = trace.marker;
if(marker) {
mergeArray(marker.opacity, cd, 'mo');
mergeArray(marker.color, cd, 'mc');
var markerLine = marker.line;
if(markerLine) {
mergeArray(markerLine.color, cd, 'mlc');
mergeArray(markerLine.width, cd, 'mlw');
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 8 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var errorBarAttrs = require('../../components/errorbars/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var fontAttrs = require('../../plots/font_attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var extendDeep = require('../../lib/extend').extendDeep;
var textFontAttrs = extendDeep({}, fontAttrs);
textFontAttrs.family.arrayOk = true;
textFontAttrs.size.arrayOk = true;
textFontAttrs.color.arrayOk = true;
var scatterMarkerAttrs = scatterAttrs.marker;
var scatterMarkerLineAttrs = scatterMarkerAttrs.line;
var markerLineWidth = extendFlat({},
scatterMarkerLineAttrs.width, { dflt: 0 });
var markerLine = extendFlat({}, {
width: markerLineWidth
}, colorAttributes('marker.line'));
var marker = extendFlat({}, {
line: markerLine
}, colorAttributes('marker'), {
showscale: scatterMarkerAttrs.showscale,
colorbar: colorbarAttrs
});
module.exports = {
x: scatterAttrs.x,
x0: scatterAttrs.x0,
dx: scatterAttrs.dx,
y: scatterAttrs.y,
y0: scatterAttrs.y0,
dy: scatterAttrs.dy,
text: scatterAttrs.text,
hovertext: scatterAttrs.hovertext,
textposition: {
valType: 'enumerated',
role: 'info',
values: ['inside', 'outside', 'auto', 'none'],
dflt: 'none',
arrayOk: true,
description: [
'Specifies the location of the `text`.',
'*inside* positions `text` inside, next to the bar end',
'(rotated and scaled if needed).',
'*outside* positions `text` outside, next to the bar end',
'(scaled if needed).',
'*auto* positions `text` inside or outside',
'so that `text` size is maximized.'
].join(' ')
},
textfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text`.'
}),
insidetextfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text` lying inside the bar.'
}),
outsidetextfont: extendFlat({}, textFontAttrs, {
description: 'Sets the font used for `text` lying outside the bar.'
}),
orientation: {
valType: 'enumerated',
role: 'info',
values: ['v', 'h'],
description: [
'Sets the orientation of the bars.',
'With *v* (*h*), the value of the each bar spans',
'along the vertical (horizontal).'
].join(' ')
},
base: {
valType: 'any',
dflt: null,
arrayOk: true,
role: 'info',
description: [
'Sets where the bar base is drawn (in position axis units).',
'In *stack* or *relative* barmode,',
'traces that set *base* will be excluded',
'and drawn in *overlay* mode instead.'
].join(' ')
},
offset: {
valType: 'number',
dflt: null,
arrayOk: true,
role: 'info',
description: [
'Shifts the position where the bar is drawn',
'(in position axis units).',
'In *group* barmode,',
'traces that set *offset* will be excluded',
'and drawn in *overlay* mode instead.'
].join(' ')
},
width: {
valType: 'number',
dflt: null,
min: 0,
arrayOk: true,
role: 'info',
description: [
'Sets the bar width (in position axis units).'
].join(' ')
},
marker: marker,
r: scatterAttrs.r,
t: scatterAttrs.t,
error_y: errorBarAttrs,
error_x: errorBarAttrs,
_deprecated: {
bardir: {
valType: 'enumerated',
role: 'info',
values: ['v', 'h'],
description: 'Renamed to `orientation`.'
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Fx = require('../../plots/cartesian/graph_interact');
var ErrorBars = require('../../components/errorbars');
var Color = require('../../components/color');
module.exports = function hoverPoints(pointData, xval, yval, hovermode) {
var cd = pointData.cd;
var trace = cd[0].trace;
var t = cd[0].t;
var xa = pointData.xa;
var ya = pointData.ya;
var posVal, thisBarMinPos, thisBarMaxPos, minPos, maxPos, dx, dy;
var positionFn = function(di) {
return Fx.inbox(minPos(di) - posVal, maxPos(di) - posVal);
};
if(trace.orientation === 'h') {
posVal = yval;
thisBarMinPos = function(di) { return di.y - di.w / 2; };
thisBarMaxPos = function(di) { return di.y + di.w / 2; };
dx = function(di) {
// add a gradient so hovering near the end of a
// bar makes it a little closer match
return Fx.inbox(di.b - xval, di.x - xval) + (di.x - xval) / (di.x - di.b);
};
dy = positionFn;
}
else {
posVal = xval;
thisBarMinPos = function(di) { return di.x - di.w / 2; };
thisBarMaxPos = function(di) { return di.x + di.w / 2; };
dy = function(di) {
return Fx.inbox(di.b - yval, di.y - yval) + (di.y - yval) / (di.y - di.b);
};
dx = positionFn;
}
minPos = (hovermode === 'closest') ?
thisBarMinPos :
function(di) {
/*
* In compare mode, accept a bar if you're on it *or* its group.
* Nearly always it's the group that matters, but in case the bar
* was explicitly set wider than its group we'd better accept the
* whole bar.
*/
return Math.min(thisBarMinPos(di), di.p - t.bargroupwidth / 2);
};
maxPos = (hovermode === 'closest') ?
thisBarMaxPos :
function(di) {
return Math.max(thisBarMaxPos(di), di.p + t.bargroupwidth / 2);
};
var distfn = Fx.getDistanceFunction(hovermode, dx, dy);
Fx.getClosest(cd, distfn, pointData);
// skip the rest (for this trace) if we didn't find a close point
if(pointData.index === false) return;
// the closest data point
var index = pointData.index,
di = cd[index],
mc = di.mcc || trace.marker.color,
mlc = di.mlcc || trace.marker.line.color,
mlw = di.mlw || trace.marker.line.width;
if(Color.opacity(mc)) pointData.color = mc;
else if(Color.opacity(mlc) && mlw) pointData.color = mlc;
var size = (trace.base) ? di.b + di.s : di.s;
if(trace.orientation === 'h') {
pointData.x0 = pointData.x1 = xa.c2p(di.x, true);
pointData.xLabelVal = size;
pointData.y0 = ya.c2p(minPos(di), true);
pointData.y1 = ya.c2p(maxPos(di), true);
pointData.yLabelVal = di.p;
}
else {
pointData.y0 = pointData.y1 = ya.c2p(di.y, true);
pointData.yLabelVal = size;
pointData.x0 = xa.c2p(minPos(di), true);
pointData.x1 = xa.c2p(maxPos(di), true);
pointData.xLabelVal = di.p;
}
if(di.htx) pointData.text = di.htx;
else if(trace.hovertext) pointData.text = trace.hovertext;
else if(di.tx) pointData.text = di.tx;
else if(trace.text) pointData.text = trace.text;
ErrorBars.hoverInfo(di, trace, pointData);
return [pointData];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Bar = {};
Bar.attributes = require('./attributes');
Bar.layoutAttributes = require('./layout_attributes');
Bar.supplyDefaults = require('./defaults');
Bar.supplyLayoutDefaults = require('./layout_defaults');
Bar.calc = require('./calc');
Bar.setPositions = require('./set_positions');
Bar.colorbar = require('../scatter/colorbar');
Bar.arraysToCalcdata = require('./arrays_to_calcdata');
Bar.plot = require('./plot');
Bar.style = require('./style');
Bar.hoverPoints = require('./hover');
Bar.moduleType = 'trace';
Bar.name = 'bar';
Bar.basePlotModule = require('../../plots/cartesian');
Bar.categories = ['cartesian', 'bar', 'oriented', 'markerColorscale', 'errorBarsOK', 'showLegend'];
Bar.meta = {
description: [
'The data visualized by the span of the bars is set in `y`',
'if `orientation` is set th *v* (the default)',
'and the labels are set in `x`.',
'By setting `orientation` to *h*, the roles are interchanged.'
].join(' ')
};
module.exports = Bar;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var BADNUM = require('../../constants/numerical').BADNUM;
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var Sieve = require('./sieve.js');
/*
* Bar chart stacking/grouping positioning and autoscaling calculations
* for each direction separately calculate the ranges and positions
* note that this handles histograms too
* now doing this one subplot at a time
*/
module.exports = function setPositions(gd, plotinfo) {
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
var fullTraces = gd._fullData,
calcTraces = gd.calcdata,
calcTracesHorizontal = [],
calcTracesVertical = [],
i;
for(i = 0; i < fullTraces.length; i++) {
var fullTrace = fullTraces[i];
if(
fullTrace.visible === true &&
Registry.traceIs(fullTrace, 'bar') &&
fullTrace.xaxis === xa._id &&
fullTrace.yaxis === ya._id
) {
if(fullTrace.orientation === 'h') {
calcTracesHorizontal.push(calcTraces[i]);
}
else {
calcTracesVertical.push(calcTraces[i]);
}
}
}
setGroupPositions(gd, xa, ya, calcTracesVertical);
setGroupPositions(gd, ya, xa, calcTracesHorizontal);
};
function setGroupPositions(gd, pa, sa, calcTraces) {
if(!calcTraces.length) return;
var barmode = gd._fullLayout.barmode,
overlay = (barmode === 'overlay'),
group = (barmode === 'group'),
excluded,
included,
i, calcTrace, fullTrace;
if(overlay) {
setGroupPositionsInOverlayMode(gd, pa, sa, calcTraces);
}
else if(group) {
// exclude from the group those traces for which the user set an offset
excluded = [];
included = [];
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
fullTrace = calcTrace[0].trace;
if(fullTrace.offset === undefined) included.push(calcTrace);
else excluded.push(calcTrace);
}
if(included.length) {
setGroupPositionsInGroupMode(gd, pa, sa, included);
}
if(excluded.length) {
setGroupPositionsInOverlayMode(gd, pa, sa, excluded);
}
}
else {
// exclude from the stack those traces for which the user set a base
excluded = [];
included = [];
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
fullTrace = calcTrace[0].trace;
if(fullTrace.base === undefined) included.push(calcTrace);
else excluded.push(calcTrace);
}
if(included.length) {
setGroupPositionsInStackOrRelativeMode(gd, pa, sa, included);
}
if(excluded.length) {
setGroupPositionsInOverlayMode(gd, pa, sa, excluded);
}
}
}
function setGroupPositionsInOverlayMode(gd, pa, sa, calcTraces) {
var barnorm = gd._fullLayout.barnorm,
separateNegativeValues = false,
dontMergeOverlappingData = !barnorm;
// update position axis and set bar offsets and widths
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i];
var sieve = new Sieve(
[calcTrace], separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidth(gd, pa, sieve);
// set bar bases and sizes, and update size axis
//
// (note that `setGroupPositionsInOverlayMode` handles the case barnorm
// is defined, because this function is also invoked for traces that
// can't be grouped or stacked)
if(barnorm) {
sieveBars(gd, sa, sieve);
normalizeBars(gd, sa, sieve);
}
else {
setBaseAndTop(gd, sa, sieve);
}
}
}
function setGroupPositionsInGroupMode(gd, pa, sa, calcTraces) {
var fullLayout = gd._fullLayout,
barnorm = fullLayout.barnorm,
separateNegativeValues = false,
dontMergeOverlappingData = !barnorm,
sieve = new Sieve(
calcTraces, separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidthInGroupMode(gd, pa, sieve);
// set bar bases and sizes, and update size axis
if(barnorm) {
sieveBars(gd, sa, sieve);
normalizeBars(gd, sa, sieve);
}
else {
setBaseAndTop(gd, sa, sieve);
}
}
function setGroupPositionsInStackOrRelativeMode(gd, pa, sa, calcTraces) {
var fullLayout = gd._fullLayout,
barmode = fullLayout.barmode,
stack = (barmode === 'stack'),
relative = (barmode === 'relative'),
barnorm = gd._fullLayout.barnorm,
separateNegativeValues = relative,
dontMergeOverlappingData = !(barnorm || stack || relative),
sieve = new Sieve(
calcTraces, separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidth(gd, pa, sieve);
// set bar bases and sizes, and update size axis
stackBars(gd, sa, sieve);
// flag the outmost bar (for text display purposes)
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i];
for(var j = 0; j < calcTrace.length; j++) {
var bar = calcTrace[j];
if(bar.s === BADNUM) continue;
var isOutmostBar = ((bar.b + bar.s) === sieve.get(bar.p, bar.s));
if(isOutmostBar) bar._outmost = true;
}
}
// Note that marking the outmost bars has to be done
// before `normalizeBars` changes `bar.b` and `bar.s`.
if(barnorm) normalizeBars(gd, sa, sieve);
}
function setOffsetAndWidth(gd, pa, sieve) {
var fullLayout = gd._fullLayout,
bargap = fullLayout.bargap,
bargroupgap = fullLayout.bargroupgap,
minDiff = sieve.minDiff,
calcTraces = sieve.traces,
i, calcTrace, calcTrace0,
t;
// set bar offsets and widths
var barGroupWidth = minDiff * (1 - bargap),
barWidthPlusGap = barGroupWidth,
barWidth = barWidthPlusGap * (1 - bargroupgap);
// computer bar group center and bar offset
var offsetFromCenter = -barWidth / 2;
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
// store bar width and offset for this trace
t = calcTrace0.t;
t.barwidth = barWidth;
t.poffset = offsetFromCenter;
t.bargroupwidth = barGroupWidth;
}
// stack bars that only differ by rounding
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
// if defined, apply trace offset and width
applyAttributes(sieve);
// store the bar center in each calcdata item
setBarCenterAndWidth(gd, pa, sieve);
// update position axes
updatePositionAxis(gd, pa, sieve);
}
function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
var fullLayout = gd._fullLayout,
bargap = fullLayout.bargap,
bargroupgap = fullLayout.bargroupgap,
positions = sieve.positions,
distinctPositions = sieve.distinctPositions,
minDiff = sieve.minDiff,
calcTraces = sieve.traces,
i, calcTrace, calcTrace0,
t;
// if there aren't any overlapping positions,
// let them have full width even if mode is group
var overlap = (positions.length !== distinctPositions.length);
var nTraces = calcTraces.length,
barGroupWidth = minDiff * (1 - bargap),
barWidthPlusGap = (overlap) ? barGroupWidth / nTraces : barGroupWidth,
barWidth = barWidthPlusGap * (1 - bargroupgap);
for(i = 0; i < nTraces; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
// computer bar group center and bar offset
var offsetFromCenter = (overlap) ?
((2 * i + 1 - nTraces) * barWidthPlusGap - barWidth) / 2 :
-barWidth / 2;
// store bar width and offset for this trace
t = calcTrace0.t;
t.barwidth = barWidth;
t.poffset = offsetFromCenter;
t.bargroupwidth = barGroupWidth;
}
// stack bars that only differ by rounding
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
// if defined, apply trace width
applyAttributes(sieve);
// store the bar center in each calcdata item
setBarCenterAndWidth(gd, pa, sieve);
// update position axes
updatePositionAxis(gd, pa, sieve, overlap);
}
function applyAttributes(sieve) {
var calcTraces = sieve.traces,
i, calcTrace, calcTrace0, fullTrace,
j,
t;
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
fullTrace = calcTrace0.trace;
t = calcTrace0.t;
var offset = fullTrace.offset,
initialPoffset = t.poffset,
newPoffset;
if(Array.isArray(offset)) {
// if offset is an array, then clone it into t.poffset.
newPoffset = offset.slice(0, calcTrace.length);
// guard against non-numeric items
for(j = 0; j < newPoffset.length; j++) {
if(!isNumeric(newPoffset[j])) {
newPoffset[j] = initialPoffset;
}
}
// if the length of the array is too short,
// then extend it with the initial value of t.poffset
for(j = newPoffset.length; j < calcTrace.length; j++) {
newPoffset.push(initialPoffset);
}
t.poffset = newPoffset;
}
else if(offset !== undefined) {
t.poffset = offset;
}
var width = fullTrace.width,
initialBarwidth = t.barwidth;
if(Array.isArray(width)) {
// if width is an array, then clone it into t.barwidth.
var newBarwidth = width.slice(0, calcTrace.length);
// guard against non-numeric items
for(j = 0; j < newBarwidth.length; j++) {
if(!isNumeric(newBarwidth[j])) newBarwidth[j] = initialBarwidth;
}
// if the length of the array is too short,
// then extend it with the initial value of t.barwidth
for(j = newBarwidth.length; j < calcTrace.length; j++) {
newBarwidth.push(initialBarwidth);
}
t.barwidth = newBarwidth;
// if user didn't set offset,
// then correct t.poffset to ensure bars remain centered
if(offset === undefined) {
newPoffset = [];
for(j = 0; j < calcTrace.length; j++) {
newPoffset.push(
initialPoffset + (initialBarwidth - newBarwidth[j]) / 2
);
}
t.poffset = newPoffset;
}
}
else if(width !== undefined) {
t.barwidth = width;
// if user didn't set offset,
// then correct t.poffset to ensure bars remain centered
if(offset === undefined) {
t.poffset = initialPoffset + (initialBarwidth - width) / 2;
}
}
}
}
function setBarCenterAndWidth(gd, pa, sieve) {
var calcTraces = sieve.traces,
pLetter = getAxisLetter(pa);
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i],
t = calcTrace[0].t,
poffset = t.poffset,
poffsetIsArray = Array.isArray(poffset),
barwidth = t.barwidth,
barwidthIsArray = Array.isArray(barwidth);
for(var j = 0; j < calcTrace.length; j++) {
var calcBar = calcTrace[j];
// store the actual bar width and position, for use by hover
var width = calcBar.w = (barwidthIsArray) ? barwidth[j] : barwidth;
calcBar[pLetter] = calcBar.p +
((poffsetIsArray) ? poffset[j] : poffset) +
width / 2;
}
}
}
function updatePositionAxis(gd, pa, sieve, allowMinDtick) {
var calcTraces = sieve.traces,
distinctPositions = sieve.distinctPositions,
distinctPositions0 = distinctPositions[0],
minDiff = sieve.minDiff,
vpad = minDiff / 2;
Axes.minDtick(pa, minDiff, distinctPositions0, allowMinDtick);
// If the user set the bar width or the offset,
// then bars can be shifted away from their positions
// and widths can be larger than minDiff.
//
// Here, we compute pMin and pMax to expand the position axis,
// so that all bars are fully within the axis range.
var pMin = Math.min.apply(Math, distinctPositions) - vpad,
pMax = Math.max.apply(Math, distinctPositions) + vpad;
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i],
calcTrace0 = calcTrace[0],
fullTrace = calcTrace0.trace;
if(fullTrace.width === undefined && fullTrace.offset === undefined) {
continue;
}
var t = calcTrace0.t,
poffset = t.poffset,
barwidth = t.barwidth,
poffsetIsArray = Array.isArray(poffset),
barwidthIsArray = Array.isArray(barwidth);
for(var j = 0; j < calcTrace.length; j++) {
var calcBar = calcTrace[j],
calcBarOffset = (poffsetIsArray) ? poffset[j] : poffset,
calcBarWidth = (barwidthIsArray) ? barwidth[j] : barwidth,
p = calcBar.p,
l = p + calcBarOffset,
r = l + calcBarWidth;
pMin = Math.min(pMin, l);
pMax = Math.max(pMax, r);
}
}
Axes.expand(pa, [pMin, pMax], {padded: false});
}
function expandRange(range, newValue) {
if(isNumeric(range[0])) range[0] = Math.min(range[0], newValue);
else range[0] = newValue;
if(isNumeric(range[1])) range[1] = Math.max(range[1], newValue);
else range[1] = newValue;
}
function setBaseAndTop(gd, sa, sieve) {
// store these bar bases and tops in calcdata
// and make sure the size axis includes zero,
// along with the bases and tops of each bar.
var traces = sieve.traces,
sLetter = getAxisLetter(sa),
s0 = sa.l2c(sa.c2l(0)),
sRange = [s0, s0];
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j],
barBase = bar.b,
barTop = barBase + bar.s;
bar[sLetter] = barTop;
if(isNumeric(sa.c2l(barTop))) expandRange(sRange, barTop);
if(isNumeric(sa.c2l(barBase))) expandRange(sRange, barBase);
}
}
Axes.expand(sa, sRange, {tozero: true, padded: true});
}
function stackBars(gd, sa, sieve) {
var fullLayout = gd._fullLayout,
barnorm = fullLayout.barnorm,
sLetter = getAxisLetter(sa),
traces = sieve.traces,
i, trace,
j, bar;
var s0 = sa.l2c(sa.c2l(0)),
sRange = [s0, s0];
for(i = 0; i < traces.length; i++) {
trace = traces[i];
for(j = 0; j < trace.length; j++) {
bar = trace[j];
if(bar.s === BADNUM) continue;
// stack current bar and get previous sum
var barBase = sieve.put(bar.p, bar.b + bar.s),
barTop = barBase + bar.b + bar.s;
// store the bar base and top in each calcdata item
bar.b = barBase;
bar[sLetter] = barTop;
if(!barnorm) {
if(isNumeric(sa.c2l(barTop))) expandRange(sRange, barTop);
if(isNumeric(sa.c2l(barBase))) expandRange(sRange, barBase);
}
}
}
// if barnorm is set, let normalizeBars update the axis range
if(!barnorm) Axes.expand(sa, sRange, {tozero: true, padded: true});
}
function sieveBars(gd, sa, sieve) {
var traces = sieve.traces;
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(bar.s !== BADNUM) sieve.put(bar.p, bar.b + bar.s);
}
}
}
function normalizeBars(gd, sa, sieve) {
// Note:
//
// normalizeBars requires that either sieveBars or stackBars has been
// previously invoked.
var traces = sieve.traces,
sLetter = getAxisLetter(sa),
sTop = (gd._fullLayout.barnorm === 'fraction') ? 1 : 100,
sTiny = sTop / 1e9, // in case of rounding error in sum
sMin = sa.l2c(sa.c2l(0)),
sMax = (gd._fullLayout.barmode === 'stack') ? sTop : sMin,
sRange = [sMin, sMax],
padded = false;
function maybeExpand(newValue) {
if(isNumeric(sa.c2l(newValue)) &&
((newValue < sMin - sTiny) || (newValue > sMax + sTiny) || !isNumeric(sMin))
) {
padded = true;
expandRange(sRange, newValue);
}
}
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(bar.s === BADNUM) continue;
var scale = Math.abs(sTop / sieve.get(bar.p, bar.s));
bar.b *= scale;
bar.s *= scale;
var barBase = bar.b,
barTop = barBase + bar.s;
bar[sLetter] = barTop;
maybeExpand(barTop);
maybeExpand(barBase);
}
}
// update range of size axis
Axes.expand(sa, sRange, {tozero: true, padded: padded});
}
function getAxisLetter(ax) {
return ax._id.charAt(0);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = Sieve;
var Lib = require('../../lib');
var BADNUM = require('../../constants/numerical').BADNUM;
/**
* Helper class to sieve data from traces into bins
*
* @class
* @param {Array} traces
* Array of calculated traces
* @param {boolean} [separateNegativeValues]
* If true, then split data at the same position into a bar
* for positive values and another for negative values
* @param {boolean} [dontMergeOverlappingData]
* If true, then don't merge overlapping bars into a single bar
*/
function Sieve(traces, separateNegativeValues, dontMergeOverlappingData) {
this.traces = traces;
this.separateNegativeValues = separateNegativeValues;
this.dontMergeOverlappingData = dontMergeOverlappingData;
var positions = [];
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(bar.p !== BADNUM) positions.push(bar.p);
}
}
this.positions = positions;
var dv = Lib.distinctVals(this.positions);
this.distinctPositions = dv.vals;
this.minDiff = dv.minDiff;
this.binWidth = this.minDiff;
this.bins = {};
}
/**
* Sieve datum
*
* @method
* @param {number} position
* @param {number} value
* @returns {number} Previous bin value
*/
Sieve.prototype.put = function put(position, value) {
var label = this.getLabel(position, value),
oldValue = this.bins[label] || 0;
this.bins[label] = oldValue + value;
return oldValue;
};
/**
* Get current bin value for a given datum
*
* @method
* @param {number} position Position of datum
* @param {number} [value] Value of datum
* (required if this.separateNegativeValues is true)
* @returns {number} Current bin value
*/
Sieve.prototype.get = function put(position, value) {
var label = this.getLabel(position, value);
return this.bins[label] || 0;
};
/**
* Get bin label for a given datum
*
* @method
* @param {number} position Position of datum
* @param {number} [value] Value of datum
* (required if this.separateNegativeValues is true)
* @returns {string} Bin label
* (prefixed with a 'v' if value is negative and this.separateNegativeValues is
* true; otherwise prefixed with '^')
*/
Sieve.prototype.getLabel = function getLabel(position, value) {
var prefix = (value < 0 && this.separateNegativeValues) ? 'v' : '^',
label = (this.dontMergeOverlappingData) ?
position :
Math.round(position / this.binWidth);
return prefix + label;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
var hasColorscale = require('../../components/colorscale/has_colorscale');
var colorscaleDefaults = require('../../components/colorscale/defaults');
module.exports = function handleStyleDefaults(traceIn, traceOut, coerce, defaultColor, layout) {
coerce('marker.color', defaultColor);
if(hasColorscale(traceIn, 'marker')) {
colorscaleDefaults(
traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'}
);
}
coerce('marker.line.color', Color.defaultLine);
if(hasColorscale(traceIn, 'marker.line')) {
colorscaleDefaults(
traceIn, traceOut, layout, coerce, {prefix: 'marker.line.', cLetter: 'c'}
);
}
coerce('marker.line.width');
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 20% | (1 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 20% | (1 / 5) | |
| index.js | 12.5% | (2 / 16) | 100% | (0 / 0) | 100% | (0 / 0) | 12.5% | (2 / 16) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var colorAttrs = require('../../components/color/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var scatterMarkerAttrs = scatterAttrs.marker,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
module.exports = {
y: {
valType: 'data_array',
description: [
'Sets the y sample data or coordinates.',
'See overview for more info.'
].join(' ')
},
x: {
valType: 'data_array',
description: [
'Sets the x sample data or coordinates.',
'See overview for more info.'
].join(' ')
},
x0: {
valType: 'any',
role: 'info',
description: [
'Sets the x coordinate of the box.',
'See overview for more info.'
].join(' ')
},
y0: {
valType: 'any',
role: 'info',
description: [
'Sets the y coordinate of the box.',
'See overview for more info.'
].join(' ')
},
xcalendar: scatterAttrs.xcalendar,
ycalendar: scatterAttrs.ycalendar,
whiskerwidth: {
valType: 'number',
min: 0,
max: 1,
dflt: 0.5,
role: 'style',
description: [
'Sets the width of the whiskers relative to',
'the box\' width.',
'For example, with 1, the whiskers are as wide as the box(es).'
].join(' ')
},
boxpoints: {
valType: 'enumerated',
values: ['all', 'outliers', 'suspectedoutliers', false],
dflt: 'outliers',
role: 'style',
description: [
'If *outliers*, only the sample points lying outside the whiskers',
'are shown',
'If *suspectedoutliers*, the outlier points are shown and',
'points either less than 4*Q1-3*Q3 or greater than 4*Q3-3*Q1',
'are highlighted (see `outliercolor`)',
'If *all*, all sample points are shown',
'If *false*, only the box(es) are shown with no sample points'
].join(' ')
},
boxmean: {
valType: 'enumerated',
values: [true, 'sd', false],
dflt: false,
role: 'style',
description: [
'If *true*, the mean of the box(es)\' underlying distribution is',
'drawn as a dashed line inside the box(es).',
'If *sd* the standard deviation is also drawn.'
].join(' ')
},
jitter: {
valType: 'number',
min: 0,
max: 1,
role: 'style',
description: [
'Sets the amount of jitter in the sample points drawn.',
'If *0*, the sample points align along the distribution axis.',
'If *1*, the sample points are drawn in a random jitter of width',
'equal to the width of the box(es).'
].join(' ')
},
pointpos: {
valType: 'number',
min: -2,
max: 2,
role: 'style',
description: [
'Sets the position of the sample points in relation to the box(es).',
'If *0*, the sample points are places over the center of the box(es).',
'Positive (negative) values correspond to positions to the',
'right (left) for vertical boxes and above (below) for horizontal boxes'
].join(' ')
},
orientation: {
valType: 'enumerated',
values: ['v', 'h'],
role: 'style',
description: [
'Sets the orientation of the box(es).',
'If *v* (*h*), the distribution is visualized along',
'the vertical (horizontal).'
].join(' ')
},
marker: {
outliercolor: {
valType: 'color',
dflt: 'rgba(0, 0, 0, 0)',
role: 'style',
description: 'Sets the color of the outlier sample points.'
},
symbol: extendFlat({}, scatterMarkerAttrs.symbol,
{arrayOk: false}),
opacity: extendFlat({}, scatterMarkerAttrs.opacity,
{arrayOk: false, dflt: 1}),
size: extendFlat({}, scatterMarkerAttrs.size,
{arrayOk: false}),
color: extendFlat({}, scatterMarkerAttrs.color,
{arrayOk: false}),
line: {
color: extendFlat({}, scatterMarkerLineAttrs.color,
{arrayOk: false, dflt: colorAttrs.defaultLine}),
width: extendFlat({}, scatterMarkerLineAttrs.width,
{arrayOk: false, dflt: 0}),
outliercolor: {
valType: 'color',
role: 'style',
description: [
'Sets the border line color of the outlier sample points.',
'Defaults to marker.color'
].join(' ')
},
outlierwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: [
'Sets the border line width (in px) of the outlier sample points.'
].join(' ')
}
}
},
line: {
color: {
valType: 'color',
role: 'style',
description: 'Sets the color of line bounding the box(es).'
},
width: {
valType: 'number',
role: 'style',
min: 0,
dflt: 2,
description: 'Sets the width (in px) of line bounding the box(es).'
}
},
fillcolor: scatterAttrs.fillcolor
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Box = {};
Box.attributes = require('./attributes');
Box.layoutAttributes = require('./layout_attributes');
Box.supplyDefaults = require('./defaults');
Box.supplyLayoutDefaults = require('./layout_defaults');
Box.calc = require('./calc');
Box.setPositions = require('./set_positions');
Box.plot = require('./plot');
Box.style = require('./style');
Box.hoverPoints = require('./hover');
Box.moduleType = 'trace';
Box.name = 'box';
Box.basePlotModule = require('../../plots/cartesian');
Box.categories = ['cartesian', 'symbols', 'oriented', 'box', 'showLegend'];
Box.meta = {
description: [
'In vertical (horizontal) box plots,',
'statistics are computed using `y` (`x`) values.',
'By supplying an `x` (`y`) array, one box per distinct x (y) value',
'is drawn',
'If no `x` (`y`) {array} is provided, a single box is drawn.',
'That box position is then positioned with',
'with `name` or with `x0` (`y0`) if provided.',
'Each box spans from quartile 1 (Q1) to quartile 3 (Q3).',
'The second quartile (Q2) is marked by a line inside the box.',
'By default, the whiskers correspond to the box\' edges',
'+/- 1.5 times the interquartile range (IQR = Q3-Q1),',
'see *boxpoints* for other options.'
].join(' ')
};
module.exports = Box;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 40% | (2 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 40% | (2 / 5) | |
| index.js | 50% | (2 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 50% | (2 / 4) | |
| transform.js | 21.95% | (9 / 41) | 0% | (0 / 10) | 0% | (0 / 7) | 21.95% | (9 / 41) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var OHLCattrs = require('../ohlc/attributes');
var boxAttrs = require('../box/attributes');
var directionAttrs = {
name: OHLCattrs.increasing.name,
showlegend: OHLCattrs.increasing.showlegend,
line: {
color: Lib.extendFlat({}, boxAttrs.line.color),
width: Lib.extendFlat({}, boxAttrs.line.width)
},
fillcolor: Lib.extendFlat({}, boxAttrs.fillcolor),
};
module.exports = {
x: OHLCattrs.x,
open: OHLCattrs.open,
high: OHLCattrs.high,
low: OHLCattrs.low,
close: OHLCattrs.close,
line: {
width: Lib.extendFlat({}, boxAttrs.line.width, {
description: [
boxAttrs.line.width.description,
'Note that this style setting can also be set per',
'direction via `increasing.line.width` and',
'`decreasing.line.width`.'
].join(' ')
})
},
increasing: Lib.extendDeep({}, directionAttrs, {
line: { color: { dflt: OHLCattrs.increasing.line.color.dflt } }
}),
decreasing: Lib.extendDeep({}, directionAttrs, {
line: { color: { dflt: OHLCattrs.decreasing.line.color.dflt } }
}),
text: OHLCattrs.text,
whiskerwidth: Lib.extendFlat({}, boxAttrs.whiskerwidth, { dflt: 0 })
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var register = require('../../plot_api/register');
module.exports = {
moduleType: 'trace',
name: 'candlestick',
basePlotModule: require('../../plots/cartesian'),
categories: ['cartesian', 'showLegend', 'candlestick'],
meta: {
description: [
'The candlestick is a style of financial chart describing',
'open, high, low and close for a given `x` coordinate (most likely time).',
'The boxes represent the spread between the `open` and `close` values and',
'the lines represent the spread between the `low` and `high` values',
'Sample points where the close value is higher (lower) then the open',
'value are called increasing (decreasing).',
'By default, increasing candles are drawn in green whereas',
'decreasing are drawn in red.'
].join(' ')
},
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
};
register(require('../box'));
register(require('./transform'));
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var helpers = require('../ohlc/helpers');
exports.moduleType = 'transform';
exports.name = 'candlestick';
exports.attributes = {};
exports.supplyDefaults = function(transformIn, traceOut, layout, traceIn) {
helpers.clearEphemeralTransformOpts(traceIn);
helpers.copyOHLC(transformIn, traceOut);
return transformIn;
};
exports.transform = function transform(dataIn, state) {
var dataOut = [];
for(var i = 0; i < dataIn.length; i++) {
var traceIn = dataIn[i];
if(traceIn.type !== 'candlestick') {
dataOut.push(traceIn);
continue;
}
dataOut.push(
makeTrace(traceIn, state, 'increasing'),
makeTrace(traceIn, state, 'decreasing')
);
}
helpers.addRangeSlider(dataOut, state.layout);
return dataOut;
};
function makeTrace(traceIn, state, direction) {
var traceOut = {
type: 'box',
boxpoints: false,
visible: traceIn.visible,
hoverinfo: traceIn.hoverinfo,
opacity: traceIn.opacity,
xaxis: traceIn.xaxis,
yaxis: traceIn.yaxis,
transforms: helpers.makeTransform(traceIn, state, direction)
};
// the rest of below may not have been coerced
var directionOpts = traceIn[direction];
if(directionOpts) {
Lib.extendFlat(traceOut, {
// to make autotype catch date axes soon!!
x: traceIn.x || [0],
xcalendar: traceIn.xcalendar,
// concat low and high to get correct autorange
y: [].concat(traceIn.low).concat(traceIn.high),
whiskerwidth: traceIn.whiskerwidth,
text: traceIn.text,
name: directionOpts.name,
showlegend: directionOpts.showlegend,
line: directionOpts.line,
fillcolor: directionOpts.fillcolor
});
}
return traceOut;
}
exports.calcTransform = function calcTransform(gd, trace, opts) {
var direction = opts.direction,
filterFn = helpers.getFilterFn(direction);
var open = trace.open,
high = trace.high,
low = trace.low,
close = trace.close;
var len = open.length,
x = [],
y = [];
var appendX = trace._fullInput.x ?
function(i) {
var v = trace.x[i];
x.push(v, v, v, v, v, v);
} :
function(i) {
x.push(i, i, i, i, i, i);
};
var appendY = function(o, h, l, c) {
y.push(l, o, c, c, c, h);
};
for(var i = 0; i < len; i++) {
if(filterFn(open[i], close[i])) {
appendX(i);
appendY(open[i], high[i], low[i], close[i]);
}
}
trace.x = x;
trace.y = y;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| ab_defaults.js | 12.5% | (3 / 24) | 0% | (0 / 12) | 0% | (0 / 3) | 12.5% | (3 / 24) | |
| array_minmax.js | 11.11% | (2 / 18) | 0% | (0 / 8) | 0% | (0 / 2) | 11.11% | (2 / 18) | |
| attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| axis_aligned_line.js | 2.04% | (1 / 49) | 0% | (0 / 34) | 0% | (0 / 5) | 2.04% | (1 / 49) | |
| axis_attributes.js | 100% | (4 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (4 / 4) | |
| axis_defaults.js | 11.67% | (14 / 120) | 0% | (0 / 49) | 0% | (0 / 4) | 11.97% | (14 / 117) | |
| calc.js | 19.23% | (10 / 52) | 0% | (0 / 10) | 0% | (0 / 1) | 19.23% | (10 / 52) | |
| calc_clippath.js | 4.17% | (1 / 24) | 100% | (0 / 0) | 0% | (0 / 1) | 4.17% | (1 / 24) | |
| calc_gridlines.js | 2.87% | (5 / 174) | 0% | (0 / 48) | 0% | (0 / 13) | 2.98% | (5 / 168) | |
| calc_labels.js | 18.75% | (3 / 16) | 0% | (0 / 4) | 0% | (0 / 1) | 18.75% | (3 / 16) | |
| catmull_rom.js | 50% | (2 / 4) | 0% | (0 / 8) | 0% | (0 / 1) | 50% | (2 / 4) | |
| cheater_basis.js | 6.67% | (2 / 30) | 0% | (0 / 16) | 0% | (0 / 1) | 6.67% | (2 / 30) | |
| compute_control_points.js | 5.88% | (4 / 68) | 0% | (0 / 20) | 0% | (0 / 2) | 5.88% | (4 / 68) | |
| constants.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| create_i_derivative_evaluator.js | 1.25% | (1 / 80) | 0% | (0 / 16) | 0% | (0 / 5) | 1.32% | (1 / 76) | |
| create_j_derivative_evaluator.js | 1.25% | (1 / 80) | 0% | (0 / 16) | 0% | (0 / 5) | 1.32% | (1 / 76) | |
| create_spline_evaluator.js | 1.04% | (1 / 96) | 0% | (0 / 16) | 0% | (0 / 5) | 1.09% | (1 / 92) | |
| defaults.js | 29.63% | (8 / 27) | 0% | (0 / 12) | 0% | (0 / 2) | 29.63% | (8 / 27) | |
| has_columns.js | 50% | (1 / 2) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (1 / 2) | |
| index.js | 100% | (12 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (12 / 12) | |
| lookup_carpetid.js | 7.69% | (1 / 13) | 0% | (0 / 8) | 0% | (0 / 1) | 8.33% | (1 / 12) | |
| makepath.js | 9.09% | (1 / 11) | 0% | (0 / 10) | 0% | (0 / 1) | 10% | (1 / 10) | |
| map_1d_array.js | 11.11% | (1 / 9) | 0% | (0 / 4) | 0% | (0 / 1) | 11.11% | (1 / 9) | |
| map_2d_array.js | 7.14% | (1 / 14) | 0% | (0 / 8) | 0% | (0 / 1) | 7.14% | (1 / 14) | |
| orient_text.js | 5.56% | (1 / 18) | 0% | (0 / 6) | 0% | (0 / 1) | 5.56% | (1 / 18) | |
| plot.js | 12.26% | (13 / 106) | 0% | (0 / 14) | 0% | (0 / 11) | 12.38% | (13 / 105) | |
| set_convert.js | 5.07% | (7 / 138) | 0% | (0 / 32) | 0% | (0 / 24) | 5.15% | (7 / 136) | |
| smooth_fill_2d_array.js | 2.8% | (3 / 107) | 0% | (0 / 46) | 0% | (0 / 2) | 2.83% | (3 / 106) | |
| smooth_fill_array.js | 2.22% | (1 / 45) | 0% | (0 / 16) | 0% | (0 / 1) | 2.27% | (1 / 44) | |
| xy_defaults.js | 16.67% | (3 / 18) | 0% | (0 / 14) | 0% | (0 / 1) | 20% | (3 / 15) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var handleAxisDefaults = require('./axis_defaults');
module.exports = function handleABDefaults(traceIn, traceOut, fullLayout, coerce, dfltColor) {
var a = coerce('a');
if(!a) {
coerce('da');
coerce('a0');
}
var b = coerce('b');
if(!b) {
coerce('db');
coerce('b0');
}
mimickAxisDefaults(traceIn, traceOut, fullLayout, dfltColor);
return;
};
function mimickAxisDefaults(traceIn, traceOut, fullLayout, dfltColor) {
var axesList = ['aaxis', 'baxis'];
axesList.forEach(function(axName) {
var axLetter = axName.charAt(0);
var axIn = traceIn[axName] || {};
var axOut = {};
var defaultOptions = {
tickfont: 'x',
id: axLetter + 'axis',
letter: axLetter,
font: traceOut.font,
name: axName,
data: traceIn[axLetter],
calendar: traceOut.calendar,
dfltColor: dfltColor,
bgColor: fullLayout.paper_bgcolor,
fullLayout: fullLayout
};
handleAxisDefaults(axIn, axOut, defaultOptions);
axOut._categories = axOut._categories || [];
traceOut[axName] = axOut;
// so we don't have to repeat autotype unnecessarily,
// copy an autotype back to traceIn
if(!traceIn[axName] && axIn.type !== '-') {
traceIn[axName] = {type: axIn.type};
}
});
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function(a) { return minMax(a, 0); }; function minMax(a, depth) { // Limit to ten dimensional datasets. This seems *exceedingly* unlikely to // ever cause problems or even be a concern. It's include strictly so that // circular arrays could never cause this to loop. if(!Array.isArray(a) || depth >= 10) { return null; } var min = Infinity; var max = -Infinity; var n = a.length; for(var i = 0; i < n; i++) { var datum = a[i]; if(Array.isArray(datum)) { var result = minMax(datum, depth + 1); if(result) { min = Math.min(result[0], min); max = Math.max(result[1], max); } } else { min = Math.min(datum, min); max = Math.max(datum, max); } } return [min, max]; } |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var extendFlat = require('../../lib/extend').extendFlat;
var fontAttrs = require('../../plots/font_attributes');
var axisAttrs = require('./axis_attributes');
var colorAttrs = require('../../components/color/attributes');
module.exports = {
carpet: {
valType: 'string',
role: 'info',
description: [
'An identifier for this carpet, so that `scattercarpet` and',
'`scattercontour` traces can specify a carpet plot on which',
'they lie'
].join(' ')
},
x: {
valType: 'data_array',
description: [
'A two dimensional array of x coordinates at each carpet point.',
'If ommitted, the plot is a cheater plot and the xaxis is hidden',
'by default.'
].join(' ')
},
y: {
valType: 'data_array',
description: 'A two dimensional array of y coordinates at each carpet point.'
},
a: {
valType: 'data_array',
description: [
'An array containing values of the first parameter value'
].join(' ')
},
a0: {
valType: 'number',
dflt: 0,
role: 'info',
description: [
'Alternate to `a`.',
'Builds a linear space of a coordinates.',
'Use with `da`',
'where `a0` is the starting coordinate and `da` the step.'
].join(' ')
},
da: {
valType: 'number',
dflt: 1,
role: 'info',
description: [
'Sets the a coordinate step.',
'See `a0` for more info.'
].join(' ')
},
b: {
valType: 'data_array',
description: 'A two dimensional array of y coordinates at each carpet point.'
},
b0: {
valType: 'number',
dflt: 0,
role: 'info',
description: [
'Alternate to `b`.',
'Builds a linear space of a coordinates.',
'Use with `db`',
'where `b0` is the starting coordinate and `db` the step.'
].join(' ')
},
db: {
valType: 'number',
dflt: 1,
role: 'info',
description: [
'Sets the b coordinate step.',
'See `b0` for more info.'
].join(' ')
},
cheaterslope: {
valType: 'number',
role: 'info',
dflt: 1,
description: [
'The shift applied to each successive row of data in creating a cheater plot.',
'Only used if `x` is been ommitted.'
].join(' ')
},
aaxis: extendFlat({}, axisAttrs),
baxis: extendFlat({}, axisAttrs),
font: {
family: extendFlat({}, fontAttrs.family, {
dflt: '"Open Sans", verdana, arial, sans-serif'
}),
size: extendFlat({}, fontAttrs.size, {
dflt: 12
}),
color: extendFlat({}, fontAttrs.color, {
dflt: colorAttrs.defaultLine
}),
},
color: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: [
'Sets default for all colors associated with this axis',
'all at once: line, font, tick, and grid colors.',
'Grid color is lightened by blending this with the plot background',
'Individual pieces can override this.'
].join(' ')
},
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* This function retrns a set of control points that define a curve aligned along * either the a or b axis. Exactly one of a or b must be an array defining the range * spanned. * * Honestly this is the most complicated function I've implemente here so far because * of the way it handles knot insertion and direction/axis-agnostic slices. */ module.exports = function(carpet, carpetcd, a, b) { var idx, tangent, tanIsoIdx, tanIsoPar, segment, refidx; var p0, p1, v0, v1, start, end, range; var axis = Array.isArray(a) ? 'a' : 'b'; var ax = axis === 'a' ? carpet.aaxis : carpet.baxis; var smoothing = ax.smoothing; var toIdx = axis === 'a' ? carpet.a2i : carpet.b2j; var pt = axis === 'a' ? a : b; var iso = axis === 'a' ? b : a; var n = axis === 'a' ? carpetcd.a.length : carpetcd.b.length; var m = axis === 'a' ? carpetcd.b.length : carpetcd.a.length; var isoIdx = Math.floor(axis === 'a' ? carpet.b2j(iso) : carpet.a2i(iso)); var xy = axis === 'a' ? function(value) { return carpet.evalxy([], value, isoIdx); } : function(value) { return carpet.evalxy([], isoIdx, value); }; if(smoothing) { tanIsoIdx = Math.max(0, Math.min(m - 2, isoIdx)); tanIsoPar = isoIdx - tanIsoIdx; tangent = axis === 'a' ? function(i, ti) { return carpet.dxydi([], i, tanIsoIdx, ti, tanIsoPar); } : function(j, tj) { return carpet.dxydj([], tanIsoIdx, j, tanIsoPar, tj); }; } var vstart = toIdx(pt[0]); var vend = toIdx(pt[1]); // So that we can make this work in two directions, flip all of the // math functions if the direction is from higher to lower indices: // // Note that the tolerance is directional! var dir = vstart < vend ? 1 : -1; var tol = (vend - vstart) * 1e-8; var dirfloor = dir > 0 ? Math.floor : Math.ceil; var dirceil = dir > 0 ? Math.ceil : Math.floor; var dirmin = dir > 0 ? Math.min : Math.max; var dirmax = dir > 0 ? Math.max : Math.min; var idx0 = dirfloor(vstart + tol); var idx1 = dirceil(vend - tol); p0 = xy(vstart); var segments = [[p0]]; for(idx = idx0; idx * dir < idx1 * dir; idx += dir) { segment = []; start = dirmax(vstart, idx); end = dirmin(vend, idx + dir); range = end - start; // In order to figure out which cell we're in for the derivative (remember, // the derivatives are *not* constant across grid lines), let's just average // the start and end points. This cuts out just a tiny bit of logic and // there's really no computational difference: refidx = Math.max(0, Math.min(n - 2, Math.floor(0.5 * (start + end)))); p1 = xy(end); if(smoothing) { v0 = tangent(refidx, start - refidx); v1 = tangent(refidx, end - refidx); segment.push([ p0[0] + v0[0] / 3 * range, p0[1] + v0[1] / 3 * range ]); segment.push([ p1[0] - v1[0] / 3 * range, p1[1] - v1[1] / 3 * range ]); } segment.push(p1); segments.push(segment); p0 = p1; } return segments; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var extendFlat = require('../../lib/extend').extendFlat;
var fontAttrs = require('../../plots/font_attributes');
var colorAttrs = require('../../components/color/attributes');
module.exports = {
color: {
valType: 'color',
role: 'style',
description: [
'Sets default for all colors associated with this axis',
'all at once: line, font, tick, and grid colors.',
'Grid color is lightened by blending this with the plot background',
'Individual pieces can override this.'
].join(' ')
},
smoothing: {
valType: 'number',
dflt: 1,
min: 0,
max: 1.3,
role: 'info'
},
title: {
valType: 'string',
role: 'info',
description: 'Sets the title of this axis.'
},
titlefont: extendFlat({}, fontAttrs, {
description: [
'Sets this axis\' title font.'
].join(' ')
}),
titleoffset: {
valType: 'number',
role: 'info',
dflt: 10,
description: [
'An additional amount by which to offset the title from the tick',
'labels, given in pixels'
].join(' '),
},
type: {
valType: 'enumerated',
// '-' means we haven't yet run autotype or couldn't find any data
// it gets turned into linear in gd._fullLayout but not copied back
// to gd.data like the others are.
values: ['-', 'linear', 'date', 'category'],
dflt: '-',
role: 'info',
description: [
'Sets the axis type.',
'By default, plotly attempts to determined the axis type',
'by looking into the data of the traces that referenced',
'the axis in question.'
].join(' ')
},
autorange: {
valType: 'enumerated',
values: [true, false, 'reversed'],
dflt: true,
role: 'style',
description: [
'Determines whether or not the range of this axis is',
'computed in relation to the input data.',
'See `rangemode` for more info.',
'If `range` is provided, then `autorange` is set to *false*.'
].join(' ')
},
rangemode: {
valType: 'enumerated',
values: ['normal', 'tozero', 'nonnegative'],
dflt: 'normal',
role: 'style',
description: [
'If *normal*, the range is computed in relation to the extrema',
'of the input data.',
'If *tozero*`, the range extends to 0,',
'regardless of the input data',
'If *nonnegative*, the range is non-negative,',
'regardless of the input data.'
].join(' ')
},
range: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'any'},
{valType: 'any'}
],
description: [
'Sets the range of this axis.',
'If the axis `type` is *log*, then you must take the log of your',
'desired range (e.g. to set the range from 1 to 100,',
'set the range from 0 to 2).',
'If the axis `type` is *date*, it should be date strings,',
'like date data, though Date objects and unix milliseconds',
'will be accepted and converted to strings.',
'If the axis `type` is *category*, it should be numbers,',
'using the scale where each category is assigned a serial',
'number from zero in the order it appears.'
].join(' ')
},
fixedrange: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'Determines whether or not this axis is zoom-able.',
'If true, then zoom is disabled.'
].join(' ')
},
cheatertype: {
valType: 'enumerated',
values: ['index', 'value'],
dflt: 'value',
role: 'info'
},
tickmode: {
valType: 'enumerated',
values: ['linear', 'array'],
dflt: 'array',
role: 'info',
},
nticks: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'style',
description: [
'Specifies the maximum number of ticks for the particular axis.',
'The actual number of ticks will be chosen automatically to be',
'less than or equal to `nticks`.',
'Has an effect only if `tickmode` is set to *auto*.'
].join(' ')
},
tickvals: {
valType: 'data_array',
description: [
'Sets the values at which ticks on this axis appear.',
'Only has an effect if `tickmode` is set to *array*.',
'Used with `ticktext`.'
].join(' ')
},
ticktext: {
valType: 'data_array',
description: [
'Sets the text displayed at the ticks position via `tickvals`.',
'Only has an effect if `tickmode` is set to *array*.',
'Used with `tickvals`.'
].join(' ')
},
showticklabels: {
valType: 'enumerated',
values: ['start', 'end', 'both', 'none'],
dflt: 'start',
role: 'style',
description: [
'Determines whether axis labels are drawn on the low side,',
'the high side, both, or neither side of the axis.'
].join(' ')
},
tickfont: extendFlat({}, fontAttrs, {
description: 'Sets the tick font.'
}),
tickangle: {
valType: 'angle',
dflt: 'auto',
role: 'style',
description: [
'Sets the angle of the tick labels with respect to the horizontal.',
'For example, a `tickangle` of -90 draws the tick labels',
'vertically.'
].join(' ')
},
tickprefix: {
valType: 'string',
dflt: '',
role: 'style',
description: 'Sets a tick label prefix.'
},
showtickprefix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: [
'If *all*, all tick labels are displayed with a prefix.',
'If *first*, only the first tick is displayed with a prefix.',
'If *last*, only the last tick is displayed with a suffix.',
'If *none*, tick prefixes are hidden.'
].join(' ')
},
ticksuffix: {
valType: 'string',
dflt: '',
role: 'style',
description: 'Sets a tick label suffix.'
},
showticksuffix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: 'Same as `showtickprefix` but for tick suffixes.'
},
showexponent: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
description: [
'If *all*, all exponents are shown besides their significands.',
'If *first*, only the exponent of the first tick is shown.',
'If *last*, only the exponent of the last tick is shown.',
'If *none*, no exponents appear.'
].join(' ')
},
exponentformat: {
valType: 'enumerated',
values: ['none', 'e', 'E', 'power', 'SI', 'B'],
dflt: 'B',
role: 'style',
description: [
'Determines a formatting rule for the tick exponents.',
'For example, consider the number 1,000,000,000.',
'If *none*, it appears as 1,000,000,000.',
'If *e*, 1e+9.',
'If *E*, 1E+9.',
'If *power*, 1x10^9 (with 9 in a super script).',
'If *SI*, 1G.',
'If *B*, 1B.'
].join(' ')
},
separatethousands: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'If "true", even 4-digit integers are separated'
].join(' ')
},
tickformat: {
valType: 'string',
dflt: '',
role: 'style',
description: [
'Sets the tick label formatting rule using d3 formatting mini-languages',
'which are very similar to those in Python. For numbers, see:',
'https://github.com/d3/d3-format/blob/master/README.md#locale_format',
'And for dates see:',
'https://github.com/d3/d3-time-format/blob/master/README.md#locale_format',
'We add one item to d3\'s date formatter: *%{n}f* for fractional seconds',
'with n digits. For example, *2016-10-13 09:15:23.456* with tickformat',
'*%H~%M~%S.%2f* would display *09~15~23.46*'
].join(' ')
},
categoryorder: {
valType: 'enumerated',
values: [
'trace', 'category ascending', 'category descending', 'array'
/* , 'value ascending', 'value descending'*/ // value ascending / descending to be implemented later
],
dflt: 'trace',
role: 'info',
description: [
'Specifies the ordering logic for the case of categorical variables.',
'By default, plotly uses *trace*, which specifies the order that is present in the data supplied.',
'Set `categoryorder` to *category ascending* or *category descending* if order should be determined by',
'the alphanumerical order of the category names.',
/* 'Set `categoryorder` to *value ascending* or *value descending* if order should be determined by the',
'numerical order of the values.',*/ // // value ascending / descending to be implemented later
'Set `categoryorder` to *array* to derive the ordering from the attribute `categoryarray`. If a category',
'is not found in the `categoryarray` array, the sorting behavior for that attribute will be identical to',
'the *trace* mode. The unspecified categories will follow the categories in `categoryarray`.'
].join(' ')
},
categoryarray: {
valType: 'data_array',
role: 'info',
description: [
'Sets the order in which categories on this axis appear.',
'Only has an effect if `categoryorder` is set to *array*.',
'Used with `categoryorder`.'
].join(' ')
},
labelpadding: {
valType: 'integer',
role: 'style',
dflt: 10,
description: 'Extra padding between label and the axis'
},
labelprefix: {
valType: 'string',
role: 'style',
description: 'Sets a axis label prefix.'
},
labelsuffix: {
valType: 'string',
dflt: '',
role: 'style',
description: 'Sets a axis label suffix.'
},
// lines and grids
showline: {
valType: 'boolean',
dflt: false,
role: 'style',
description: [
'Determines whether or not a line bounding this axis is drawn.'
].join(' ')
},
linecolor: {
valType: 'color',
dflt: colorAttrs.defaultLine,
role: 'style',
description: 'Sets the axis line color.'
},
linewidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the axis line.'
},
gridcolor: {
valType: 'color',
role: 'style',
description: 'Sets the axis line color.'
},
gridwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the axis line.'
},
showgrid: {
valType: 'boolean',
role: 'style',
dflt: true,
description: [
'Determines whether or not grid lines are drawn.',
'If *true*, the grid lines are drawn at every tick mark.'
].join(' ')
},
minorgridcount: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'info',
description: 'Sets the number of minor grid ticks per major grid tick'
},
minorgridwidth: {
valType: 'number',
min: 0,
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the grid lines.'
},
minorgridcolor: {
valType: 'color',
dflt: colorAttrs.lightLine,
role: 'style',
description: 'Sets the color of the grid lines.'
},
startline: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not a line is drawn at along the starting value',
'of this axis.',
'If *true*, the start line is drawn on top of the grid lines.'
].join(' ')
},
startlinecolor: {
valType: 'color',
role: 'style',
description: 'Sets the line color of the start line.'
},
startlinewidth: {
valType: 'number',
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the start line.'
},
endline: {
valType: 'boolean',
role: 'style',
description: [
'Determines whether or not a line is drawn at along the final value',
'of this axis.',
'If *true*, the end line is drawn on top of the grid lines.'
].join(' ')
},
endlinewidth: {
valType: 'number',
dflt: 1,
role: 'style',
description: 'Sets the width (in px) of the end line.'
},
endlinecolor: {
valType: 'color',
role: 'style',
description: 'Sets the line color of the end line.'
},
tick0: {
valType: 'number',
min: 0,
dflt: 0,
role: 'info',
description: 'The starting index of grid lines along the axis'
},
dtick: {
valType: 'number',
min: 0,
dflt: 1,
role: 'info',
description: 'The stride between grid lines along the axis'
},
arraytick0: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'info',
description: 'The starting index of grid lines along the axis'
},
arraydtick: {
valType: 'integer',
min: 1,
dflt: 1,
role: 'info',
description: 'The stride between grid lines along the axis'
},
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var carpetAttrs = require('./attributes');
var addOpacity = require('../../components/color').addOpacity;
var Registry = require('../../registry');
var Lib = require('../../lib');
var handleTickValueDefaults = require('../../plots/cartesian/tick_value_defaults');
var handleTickLabelDefaults = require('../../plots/cartesian/tick_label_defaults');
var handleCategoryOrderDefaults = require('../../plots/cartesian/category_order_defaults');
var setConvert = require('../../plots/cartesian/set_convert');
var orderedCategories = require('../../plots/cartesian/ordered_categories');
var autoType = require('../../plots/cartesian/axis_autotype');
/**
* options: object containing:
*
* letter: 'x' or 'y'
* title: name of the axis (ie 'Colorbar') to go in default title
* name: axis object name (ie 'xaxis') if one should be stored
* font: the default font to inherit
* outerTicks: boolean, should ticks default to outside?
* showGrid: boolean, should gridlines be shown by default?
* noHover: boolean, this axis doesn't support hover effects?
* data: the plot data to use in choosing auto type
* bgColor: the plot background color, to calculate default gridline colors
*/
module.exports = function handleAxisDefaults(containerIn, containerOut, options) {
var letter = options.letter,
font = options.font || {},
attributes = carpetAttrs[letter + 'axis'];
options.noHover = true;
function coerce(attr, dflt) {
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
}
function coerce2(attr, dflt) {
return Lib.coerce2(containerIn, containerOut, attributes, attr, dflt);
}
// set up some private properties
if(options.name) {
containerOut._name = options.name;
containerOut._id = options.name;
}
// now figure out type and do some more initialization
var axType = coerce('type');
if(axType === '-') {
if(options.data) setAutoType(containerOut, options.data);
if(containerOut.type === '-') {
containerOut.type = 'linear';
}
else {
// copy autoType back to input axis
// note that if this object didn't exist
// in the input layout, we have to put it in
// this happens in the main supplyDefaults function
axType = containerIn.type = containerOut.type;
}
}
coerce('smoothing');
coerce('cheatertype');
coerce('showticklabels');
coerce('labelprefix', letter + ' = ');
coerce('labelsuffix');
coerce('showtickprefix');
coerce('showticksuffix');
coerce('separatethousands');
coerce('tickformat');
coerce('exponentformat');
coerce('showexponent');
coerce('categoryorder');
coerce('tickmode');
coerce('tickvals');
coerce('ticktext');
coerce('tick0');
coerce('dtick');
if(containerOut.tickmode === 'array') {
coerce('arraytick0');
coerce('arraydtick');
}
coerce('labelpadding');
containerOut._hovertitle = letter;
if(axType === 'date') {
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(containerIn, containerOut, 'calendar', options.calendar);
}
setConvert(containerOut, options.fullLayout);
var dfltColor = coerce('color', options.dfltColor);
// if axis.color was provided, use it for fonts too; otherwise,
// inherit from global font color in case that was provided.
var dfltFontColor = (dfltColor === containerIn.color) ? dfltColor : font.color;
coerce('title');
Lib.coerceFont(coerce, 'titlefont', {
family: font.family,
size: Math.round(font.size * 1.2),
color: dfltFontColor
});
coerce('titleoffset');
coerce('tickangle');
var autoRange = coerce('autorange', !containerOut.isValidRange(containerIn.range));
if(autoRange) coerce('rangemode');
coerce('range');
containerOut.cleanRange();
coerce('fixedrange');
handleTickValueDefaults(containerIn, containerOut, coerce, axType);
handleTickLabelDefaults(containerIn, containerOut, coerce, axType, options);
handleCategoryOrderDefaults(containerIn, containerOut, coerce);
var gridColor = coerce2('gridcolor', addOpacity(dfltColor, 0.3));
var gridWidth = coerce2('gridwidth');
var showGrid = coerce('showgrid');
if(!showGrid) {
delete containerOut.gridcolor;
delete containerOut.gridwidth;
}
var startLineColor = coerce2('startlinecolor', dfltColor);
var startLineWidth = coerce2('startlinewidth', gridWidth);
var showStartLine = coerce('startline', containerOut.showgrid || !!startLineColor || !!startLineWidth);
if(!showStartLine) {
delete containerOut.startlinecolor;
delete containerOut.startlinewidth;
}
var endLineColor = coerce2('endlinecolor', dfltColor);
var endLineWidth = coerce2('endlinewidth', gridWidth);
var showEndLine = coerce('endline', containerOut.showgrid || !!endLineColor || !!endLineWidth);
if(!showEndLine) {
delete containerOut.endlinecolor;
delete containerOut.endlinewidth;
}
if(!showGrid) {
delete containerOut.gridcolor;
delete containerOut.gridWidth;
} else {
coerce('minorgridcount');
coerce('minorgridwidth', gridWidth);
coerce('minorgridcolor', addOpacity(gridColor, 0.06));
if(!containerOut.minorgridcount) {
delete containerOut.minorgridwidth;
delete containerOut.minorgridcolor;
}
}
containerOut._separators = options.fullLayout.separators;
// fill in categories
containerOut._initialCategories = axType === 'category' ?
orderedCategories(letter, containerOut.categoryorder, containerOut.categoryarray, options.data) :
[];
if(containerOut.showticklabels === 'none') {
delete containerOut.tickfont;
delete containerOut.tickangle;
delete containerOut.showexponent;
delete containerOut.exponentformat;
delete containerOut.tickformat;
delete containerOut.showticksuffix;
delete containerOut.showtickprefix;
}
if(!containerOut.showticksuffix) {
delete containerOut.ticksuffix;
}
if(!containerOut.showtickprefix) {
delete containerOut.tickprefix;
}
// It needs to be coerced, then something above overrides this deep in the axis code,
// but no, we *actually* want to coerce this.
coerce('tickmode');
if(!containerOut.title || (containerOut.title && containerOut.title.length === 0)) {
delete containerOut.titlefont;
delete containerOut.titleoffset;
}
return containerOut;
};
function setAutoType(ax, data) {
// new logic: let people specify any type they want,
// only autotype if type is '-'
if(ax.type !== '-') return;
var id = ax._id,
axLetter = id.charAt(0);
var calAttr = axLetter + 'calendar',
calendar = ax[calAttr];
ax.type = autoType(data, calendar);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Axes = require('../../plots/cartesian/axes');
var cheaterBasis = require('./cheater_basis');
var arrayMinmax = require('./array_minmax');
var map2dArray = require('./map_2d_array');
var calcGridlines = require('./calc_gridlines');
var calcLabels = require('./calc_labels');
var calcClipPath = require('./calc_clippath');
var clean2dArray = require('../heatmap/clean_2d_array');
var smoothFill2dArray = require('./smooth_fill_2d_array');
module.exports = function calc(gd, trace) {
var xa = Axes.getFromId(gd, trace.xaxis || 'x');
var ya = Axes.getFromId(gd, trace.yaxis || 'y');
var aax = trace.aaxis;
var bax = trace.baxis;
var a = trace._a = trace.a;
var b = trace._b = trace.b;
var t = {};
var x;
var y = trace.y;
if(trace._cheater) {
var avals = aax.cheatertype === 'index' ? a.length : a;
var bvals = bax.cheatertype === 'index' ? b.length : b;
trace.x = x = cheaterBasis(avals, bvals, trace.cheaterslope);
} else {
x = trace.x;
}
trace._x = trace.x = x = clean2dArray(x);
trace._y = trace.y = y = clean2dArray(y);
// Fill in any undefined values with elliptic smoothing. This doesn't take
// into account the spacing of the values. That is, the derivatives should
// be modified to use a and b values. It's not that hard, but this is already
// moderate overkill for just filling in missing values.
smoothFill2dArray(x, a, b);
smoothFill2dArray(y, a, b);
// create conversion functions that depend on the data
trace.setScale();
// Convert cartesian-space x/y coordinates to screen space pixel coordinates:
t.xp = trace.xp = map2dArray(trace.xp, x, xa.c2p);
t.yp = trace.yp = map2dArray(trace.yp, y, ya.c2p);
// This is a rather expensive scan. Nothing guarantees monotonicity,
// so we need to scan through all data to get proper ranges:
var xrange = arrayMinmax(x);
var yrange = arrayMinmax(y);
var dx = 0.5 * (xrange[1] - xrange[0]);
var xc = 0.5 * (xrange[1] + xrange[0]);
var dy = 0.5 * (yrange[1] - yrange[0]);
var yc = 0.5 * (yrange[1] + yrange[0]);
// Expand the axes to fit the plot, except just grow it by a factor of 1.3
// because the labels should be taken into account except that's difficult
// hence 1.3.
var grow = 1.3;
xrange = [xc - dx * grow, xc + dx * grow];
yrange = [yc - dy * grow, yc + dy * grow];
Axes.expand(xa, xrange, {padded: true});
Axes.expand(ya, yrange, {padded: true});
// Enumerate the gridlines, both major and minor, and store them on the trace
// object:
calcGridlines(trace, t, 'a', 'b');
calcGridlines(trace, t, 'b', 'a');
// Calculate the text labels for each major gridline and store them on the
// trace object:
calcLabels(trace, aax);
calcLabels(trace, bax);
// Tabulate points for the four segments that bound the axes so that we can
// map to pixel coordinates in the plot function and create a clip rect:
t.clipsegments = calcClipPath(trace.xctrl, trace.yctrl, aax, bax);
t.x = x;
t.y = y;
t.a = a;
t.b = b;
return [t];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function makeClipPath(xctrl, yctrl, aax, bax) { var i, x, y; var segments = []; var asmoothing = !!aax.smoothing; var bsmoothing = !!bax.smoothing; var nea1 = xctrl[0].length - 1; var neb1 = xctrl.length - 1; // Along the lower a axis: for(i = 0, x = [], y = []; i <= nea1; i++) { x[i] = xctrl[0][i]; y[i] = yctrl[0][i]; } segments.push({x: x, y: y, bicubic: asmoothing}); // Along the upper b axis: for(i = 0, x = [], y = []; i <= neb1; i++) { x[i] = xctrl[i][nea1]; y[i] = yctrl[i][nea1]; } segments.push({x: x, y: y, bicubic: bsmoothing}); // Backwards along the upper a axis: for(i = nea1, x = [], y = []; i >= 0; i--) { x[nea1 - i] = xctrl[neb1][i]; y[nea1 - i] = yctrl[neb1][i]; } segments.push({x: x, y: y, bicubic: asmoothing}); // Backwards along the lower b axis: for(i = neb1, x = [], y = []; i >= 0; i--) { x[neb1 - i] = xctrl[i][0]; y[neb1 - i] = yctrl[i][0]; } segments.push({x: x, y: y, bicubic: bsmoothing}); return segments; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Axes = require('../../plots/cartesian/axes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = function calcGridlines(trace, cd, axisLetter, crossAxisLetter) {
var i, j, j0;
var eps, bounds, n1, n2, n, value, v;
var j1, v0, v1, d;
var data = trace[axisLetter];
var axis = trace[axisLetter + 'axis'];
var gridlines = axis._gridlines = [];
var minorgridlines = axis._minorgridlines = [];
var boundarylines = axis._boundarylines = [];
var crossData = trace[crossAxisLetter];
var crossAxis = trace[crossAxisLetter + 'axis'];
if(axis.tickmode === 'array') {
axis.tickvals = [];
for(i = 0; i < data.length; i++) {
axis.tickvals.push(data[i]);
}
}
var xcp = trace.xctrl;
var ycp = trace.yctrl;
var nea = xcp[0].length;
var neb = xcp.length;
var na = trace.a.length;
var nb = trace.b.length;
Axes.calcTicks(axis);
// The default is an empty array that will cause the join to remove the gridline if
// it's just disappeared:
// axis._startline = axis._endline = [];
// If the cross axis uses bicubic interpolation, then the grid
// lines fall once every three expanded grid row/cols:
var stride = axis.smoothing ? 3 : 1;
function constructValueGridline(value) {
var i, j, j0, tj, pxy, i0, ti, xy, dxydi0, dxydi1, dxydj0, dxydj1;
var xpoints = [];
var ypoints = [];
var ret = {};
// Search for the fractional grid index giving this line:
if(axisLetter === 'b') {
// For the position we use just the i-j coordinates:
j = trace.b2j(value);
// The derivatives for catmull-rom splines are discontinuous across cell
// boundaries though, so we need to provide both the cell and the position
// within the cell separately:
j0 = Math.floor(Math.max(0, Math.min(nb - 2, j)));
tj = j - j0;
ret.length = nb;
ret.crossLength = na;
ret.xy = function(i) {
return trace.evalxy([], i, j);
};
ret.dxy = function(i0, ti) {
return trace.dxydi([], i0, j0, ti, tj);
};
for(i = 0; i < na; i++) {
i0 = Math.min(na - 2, i);
ti = i - i0;
xy = trace.evalxy([], i, j);
if(crossAxis.smoothing && i > 0) {
// First control point:
dxydi0 = trace.dxydi([], i - 1, j0, 0, tj);
xpoints.push(pxy[0] + dxydi0[0] / 3);
ypoints.push(pxy[1] + dxydi0[1] / 3);
// Second control point:
dxydi1 = trace.dxydi([], i - 1, j0, 1, tj);
xpoints.push(xy[0] - dxydi1[0] / 3);
ypoints.push(xy[1] - dxydi1[1] / 3);
}
xpoints.push(xy[0]);
ypoints.push(xy[1]);
pxy = xy;
}
} else {
i = trace.a2i(value);
i0 = Math.floor(Math.max(0, Math.min(na - 2, i)));
ti = i - i0;
ret.length = na;
ret.crossLength = nb;
ret.xy = function(j) {
return trace.evalxy([], i, j);
};
ret.dxy = function(j0, tj) {
return trace.dxydj([], i0, j0, ti, tj);
};
for(j = 0; j < nb; j++) {
j0 = Math.min(nb - 2, j);
tj = j - j0;
xy = trace.evalxy([], i, j);
if(crossAxis.smoothing && j > 0) {
// First control point:
dxydj0 = trace.dxydj([], i0, j - 1, ti, 0);
xpoints.push(pxy[0] + dxydj0[0] / 3);
ypoints.push(pxy[1] + dxydj0[1] / 3);
// Second control point:
dxydj1 = trace.dxydj([], i0, j - 1, ti, 1);
xpoints.push(xy[0] - dxydj1[0] / 3);
ypoints.push(xy[1] - dxydj1[1] / 3);
}
xpoints.push(xy[0]);
ypoints.push(xy[1]);
pxy = xy;
}
}
ret.axisLetter = axisLetter;
ret.axis = axis;
ret.crossAxis = crossAxis;
ret.value = value;
ret.constvar = crossAxisLetter;
ret.index = n;
ret.x = xpoints;
ret.y = ypoints;
ret.smoothing = crossAxis.smoothing;
return ret;
}
function constructArrayGridline(idx) {
var j, i0, j0, ti, tj;
var xpoints = [];
var ypoints = [];
var ret = {};
ret.length = data.length;
ret.crossLength = crossData.length;
if(axisLetter === 'b') {
j0 = Math.max(0, Math.min(nb - 2, idx));
tj = Math.min(1, Math.max(0, idx - j0));
ret.xy = function(i) {
return trace.evalxy([], i, idx);
};
ret.dxy = function(i0, ti) {
return trace.dxydi([], i0, j0, ti, tj);
};
// In the tickmode: array case, this operation is a simple
// transfer of data:
for(j = 0; j < nea; j++) {
xpoints[j] = xcp[idx * stride][j];
ypoints[j] = ycp[idx * stride][j];
}
} else {
i0 = Math.max(0, Math.min(na - 2, idx));
ti = Math.min(1, Math.max(0, idx - i0));
ret.xy = function(j) {
return trace.evalxy([], idx, j);
};
ret.dxy = function(j0, tj) {
return trace.dxydj([], i0, j0, ti, tj);
};
// In the tickmode: array case, this operation is a simple
// transfer of data:
for(j = 0; j < neb; j++) {
xpoints[j] = xcp[j][idx * stride];
ypoints[j] = ycp[j][idx * stride];
}
}
ret.axisLetter = axisLetter;
ret.axis = axis;
ret.crossAxis = crossAxis;
ret.value = data[idx];
ret.constvar = crossAxisLetter;
ret.index = idx;
ret.x = xpoints;
ret.y = ypoints;
ret.smoothing = crossAxis.smoothing;
return ret;
}
if(axis.tickmode === 'array') {
// var j0 = axis.startline ? 1 : 0;
// var j1 = data.length - (axis.endline ? 1 : 0);
eps = 5e-15;
bounds = [
Math.floor(((data.length - 1) - axis.arraytick0) / axis.arraydtick * (1 + eps)),
Math.ceil((- axis.arraytick0) / axis.arraydtick / (1 + eps))
].sort(function(a, b) {return a - b;});
// Unpack sorted values so we can be sure to avoid infinite loops if something
// is backwards:
n1 = bounds[0] - 1;
n2 = bounds[1] + 1;
// If the axes fall along array lines, then this is a much simpler process since
// we already have all the control points we need
for(n = n1; n < n2; n++) {
j = axis.arraytick0 + axis.arraydtick * n;
if(j < 0 || j > data.length - 1) continue;
gridlines.push(extendFlat(constructArrayGridline(j), {
color: axis.gridcolor,
width: axis.gridwidth
}));
}
for(n = n1; n < n2; n++) {
j0 = axis.arraytick0 + axis.arraydtick * n;
j1 = Math.min(j0 + axis.arraydtick, data.length - 1);
// TODO: fix the bounds computation so we don't have to do a large range and then throw
// out unneeded numbers
if(j0 < 0 || j0 > data.length - 1) continue;
if(j1 < 0 || j1 > data.length - 1) continue;
v0 = data[j0];
v1 = data[j1];
for(i = 0; i < axis.minorgridcount; i++) {
d = j1 - j0;
// TODO: fix the bounds computation so we don't have to do a large range and then throw
// out unneeded numbers
if(d <= 0) continue;
// XXX: This calculation isn't quite right. Off by one somewhere?
v = v0 + (v1 - v0) * (i + 1) / (axis.minorgridcount + 1) * (axis.arraydtick / d);
// TODO: fix the bounds computation so we don't have to do a large range and then throw
// out unneeded numbers
if(v < data[0] || v > data[data.length - 1]) continue;
minorgridlines.push(extendFlat(constructValueGridline(v), {
color: axis.minorgridcolor,
width: axis.minorgridwidth
}));
}
}
if(axis.startline) {
boundarylines.push(extendFlat(constructArrayGridline(0), {
color: axis.startlinecolor,
width: axis.startlinewidth
}));
}
if(axis.endline) {
boundarylines.push(extendFlat(constructArrayGridline(data.length - 1), {
color: axis.endlinecolor,
width: axis.endlinewidth
}));
}
} else {
// If the lines do not fall along the axes, then we have to interpolate
// the contro points and so some math to figure out where the lines are
// in the first place.
// Compute the integer boudns of tick0 + n * dtick that fall within the range
// (roughly speaking):
// Give this a nice generous epsilon. We use at as * (1 + eps) in order to make
// inequalities a little tolerant in a more or less correct manner:
eps = 5e-15;
bounds = [
Math.floor((data[data.length - 1] - axis.tick0) / axis.dtick * (1 + eps)),
Math.ceil((data[0] - axis.tick0) / axis.dtick / (1 + eps))
].sort(function(a, b) {return a - b;});
// Unpack sorted values so we can be sure to avoid infinite loops if something
// is backwards:
n1 = bounds[0];
n2 = bounds[1];
for(n = n1; n <= n2; n++) {
value = axis.tick0 + axis.dtick * n;
gridlines.push(extendFlat(constructValueGridline(value), {
color: axis.gridcolor,
width: axis.gridwidth
}));
}
for(n = n1 - 1; n < n2 + 1; n++) {
value = axis.tick0 + axis.dtick * n;
for(i = 0; i < axis.minorgridcount; i++) {
v = value + axis.dtick * (i + 1) / (axis.minorgridcount + 1);
if(v < data[0] || v > data[data.length - 1]) continue;
minorgridlines.push(extendFlat(constructValueGridline(v), {
color: axis.minorgridcolor,
width: axis.minorgridwidth
}));
}
}
if(axis.startline) {
boundarylines.push(extendFlat(constructValueGridline(data[0]), {
color: axis.startlinecolor,
width: axis.startlinewidth
}));
}
if(axis.endline) {
boundarylines.push(extendFlat(constructValueGridline(data[data.length - 1]), {
color: axis.endlinecolor,
width: axis.endlinewidth
}));
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Axes = require('../../plots/cartesian/axes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = function calcLabels(trace, axis) {
var i, tobj, prefix, suffix, gridline;
var labels = axis._labels = [];
var gridlines = axis._gridlines;
for(i = 0; i < gridlines.length; i++) {
gridline = gridlines[i];
if(['start', 'both'].indexOf(axis.showticklabels) !== -1) {
tobj = Axes.tickText(axis, gridline.value);
extendFlat(tobj, {
prefix: prefix,
suffix: suffix,
endAnchor: true,
xy: gridline.xy(0),
dxy: gridline.dxy(0, 0),
axis: gridline.axis,
length: gridline.crossAxis.length,
font: gridline.axis.tickfont,
isFirst: i === 0,
isLast: i === gridlines.length - 1
});
labels.push(tobj);
}
if(['end', 'both'].indexOf(axis.showticklabels) !== -1) {
tobj = Axes.tickText(axis, gridline.value);
extendFlat(tobj, {
endAnchor: false,
xy: gridline.xy(gridline.crossLength - 1),
dxy: gridline.dxy(gridline.crossLength - 2, 1),
axis: gridline.axis,
length: gridline.crossAxis.length,
font: gridline.axis.tickfont,
isFirst: i === 0,
isLast: i === gridlines.length - 1
});
labels.push(tobj);
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Compute the tangent vector according to catmull-rom cubic splines (centripetal, * I think). That differs from the control point in two ways: * 1. It is a vector, not a position relative to the point * 2. the vector is longer than the position relative to p1 by a factor of 3 * * Close to the boundaries, we'll use these as *quadratic control points, so that * to make a nice grid, we'll need to divide the tangent by 2 instead of 3. (The * math works out this way if you work through the bezier derivatives) */ var CatmullRomExp = 0.5; module.exports = function makeControlPoints(p0, p1, p2, smoothness) { var d1x = p0[0] - p1[0], d1y = p0[1] - p1[1], d2x = p2[0] - p1[0], d2y = p2[1] - p1[1], d1a = Math.pow(d1x * d1x + d1y * d1y, CatmullRomExp / 2), d2a = Math.pow(d2x * d2x + d2y * d2y, CatmullRomExp / 2), numx = (d2a * d2a * d1x - d1a * d1a * d2x) * smoothness, numy = (d2a * d2a * d1y - d1a * d1a * d2y) * smoothness, denom1 = d2a * (d1a + d2a) * 3, denom2 = d1a * (d1a + d2a) * 3; return [[ p1[0] + (denom1 && numx / denom1), p1[1] + (denom1 && numy / denom1) ], [ p1[0] - (denom2 && numx / denom2), p1[1] - (denom2 && numy / denom2) ]]; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isArray = require('../../lib').isArray;
/*
* Construct a 2D array of cheater values given a, b, and a slope.
* If
*/
module.exports = function(a, b, cheaterslope) {
var i, j, ascal, bscal, aval, bval;
var data = [];
var na = isArray(a) ? a.length : a;
var nb = isArray(b) ? b.length : b;
var adata = isArray(a) ? a : null;
var bdata = isArray(b) ? b : null;
// If we're using data, scale it so that for data that's just barely
// not evenly spaced, the switch to value-based indexing is continuous.
// This means evenly spaced data should look the same whether value
// or index cheatertype.
if(adata) {
ascal = (adata.length - 1) / (adata[adata.length - 1] - adata[0]) / (na - 1);
}
if(bdata) {
bscal = (bdata.length - 1) / (bdata[bdata.length - 1] - bdata[0]) / (nb - 1);
}
var xval;
var xmin = Infinity;
var xmax = -Infinity;
for(j = 0; j < nb; j++) {
data[j] = [];
bval = bdata ? (bdata[j] - bdata[0]) * bscal : j / (nb - 1);
for(i = 0; i < na; i++) {
aval = adata ? (adata[i] - adata[0]) * ascal : i / (na - 1);
xval = aval - bval * cheaterslope;
xmin = Math.min(xval, xmin);
xmax = Math.max(xval, xmax);
data[j][i] = xval;
}
}
// Normalize cheater values to the 0-1 range. This comes into play when you have
// multiple cheater plots. After careful consideration, it seems better if cheater
// values are normalized to a consistent range. Otherwise one cheater affects the
// layout of other cheaters on the same axis.
var slope = 1.0 / (xmax - xmin);
var offset = -xmin * slope;
for(j = 0; j < nb; j++) {
for(i = 0; i < na; i++) {
data[j][i] = slope * data[j][i] + offset;
}
}
return data;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var makeControlPoints = require('./catmull_rom');
var ensureArray = require('../../lib').ensureArray;
/*
* Turns a coarse grid into a fine grid with control points.
*
* Here's an ASCII representation:
*
* o ----- o ----- o ----- o
* | | | |
* | | | |
* | | | |
* o ----- o ----- o ----- o
* | | | |
* | | | |
* ^ | | | |
* | o ----- o ----- o ----- o
* b | | | | |
* | | | | |
* | | | | |
* o ----- o ----- o ----- o
* ------>
* a
*
* First of all, note that we want to do this in *cartesian* space. This means
* we might run into problems when there are extreme differences in x/y scaling,
* but the alternative is that the topology of the contours might actually be
* view-dependent, which seems worse. As a fallback, the only parameter that
* actually affects the result is the *aspect ratio*, so that we can at least
* improve the situation a bit without going all the way to screen coordinates.
*
* This function flattens the points + tangents into a slightly denser grid of
* *control points*. The resulting grid looks like this:
*
* 9 +--o-o--+ -o-o--+--o-o--+
* 8 o o o o o o o o o o
* | | | |
* 7 o o o o o o o o o o
* 6 +--o-o--+ -o-o--+--o-o--+
* 5 o o o o o o o o o o
* | | | |
* ^ 4 o o o o o o o o o o
* | 3 +--o-o--+ -o-o--+--o-o--+
* b | 2 o o o o o o o o o o
* | | | | |
* | 1 o o o o o o o o o o
* 0 +--o-o--+ -o-o--+--o-o--+
* 0 1 2 3 4 5 6 7 8 9
* ------>
* a
*
* where `o`s represent newly-computed control points. the resulting dimension is
*
* (m - 1) * 3 + 1
* = 3 * m - 2
*
* We could simply store the tangents separately, but that's a nightmare to organize
* in two dimensions since we'll be slicing grid lines in both directions and since
* that basically requires very nearly just as much storage as just storing the dense
* grid.
*
* Wow!
*/
/*
* Catmull-rom is biased at the boundaries toward the interior and we actually
* can't use catmull-rom to compute the control point closest to (but inside)
* the boundary.
*
* A note on plotly's spline interpolation. It uses the catmull rom control point
* closest to the boundary *as* a quadratic control point. This seems incorrect,
* so I've elected not to follow that. Given control points 0 and 1, regular plotly
* splines give *equivalent* cubic control points:
*
* Input:
*
* boundary
* | |
* p0 p2 p3 --> interior
* 0.0 0.667 1.0
* | |
*
* Cubic-equivalent of what plotly splines draw::
*
* boundary
* | |
* p0 p1 p2 p3 --> interior
* 0.0 0.4444 0.8888 1.0
* | |
*
* What this function fills in:
*
* boundary
* | |
* p0 p1 p2 p3 --> interior
* 0.0 0.333 0.667 1.0
* | |
*
* Parameters:
* p0: boundary point
* p2: catmull rom point based on computation at p3
* p3: first grid point
*
* Of course it works whichever way it's oriented; you just need to interpret the
* input/output accordingly.
*/
function inferCubicControlPoint(p0, p2, p3) {
// Extend p1 away from p0 by 50%. This is the equivalent quadratic point that
// would give the same slope as catmull rom at p0.
var p2e0 = -0.5 * p3[0] + 1.5 * p2[0];
var p2e1 = -0.5 * p3[1] + 1.5 * p2[1];
return [
(2 * p2e0 + p0[0]) / 3,
(2 * p2e1 + p0[1]) / 3,
];
}
module.exports = function computeControlPoints(xe, ye, x, y, asmoothing, bsmoothing) {
var i, j, ie, je, xej, yej, xj, yj, cp, p1;
// At this point, we know these dimensions are correct and representative of
// the whole 2D arrays:
var na = x[0].length;
var nb = x.length;
// (n)umber of (e)xpanded points:
var nea = asmoothing ? 3 * na - 2 : na;
var neb = bsmoothing ? 3 * nb - 2 : nb;
xe = ensureArray(xe, neb);
ye = ensureArray(ye, neb);
for(ie = 0; ie < neb; ie++) {
xe[ie] = ensureArray(xe[ie], nea);
ye[ie] = ensureArray(ye[ie], nea);
}
// This loop fills in the X'd points:
//
// . . . .
// . . . .
// | | | |
// | | | |
// X ----- X ----- X ----- X
// | | | |
// | | | |
// | | | |
// X ----- X ----- X ----- X
//
//
// ie = (i) (e)xpanded:
for(j = 0, je = 0; j < nb; j++, je += bsmoothing ? 3 : 1) {
xej = xe[je];
yej = ye[je];
xj = x[j];
yj = y[j];
// je = (j) (e)xpanded:
for(i = 0, ie = 0; i < na; i++, ie += asmoothing ? 3 : 1) {
xej[ie] = xj[i];
yej[ie] = yj[i];
}
}
if(asmoothing) {
// If there's a-smoothing, this loop fills in the X'd points with catmull-rom
// control points computed along the a-axis:
// . . . .
// . . . .
// | | | |
// | | | |
// o -Y-X- o -X-X- o -X-Y- o
// | | | |
// | | | |
// | | | |
// o -Y-X- o -X-X- o -X-Y- o
//
// i: 0 1 2 3
// ie: 0 1 3 3 4 5 6 7 8 9
//
// ------>
// a
//
for(j = 0, je = 0; j < nb; j++, je += bsmoothing ? 3 : 1) {
// Fill in the points marked X for this a-row:
for(i = 1, ie = 3; i < na - 1; i++, ie += 3) {
cp = makeControlPoints(
[x[j][i - 1], y[j][i - 1]],
[x[j][i ], y[j][i]],
[x[j][i + 1], y[j][i + 1]],
asmoothing
);
xe[je][ie - 1] = cp[0][0];
ye[je][ie - 1] = cp[0][1];
xe[je][ie + 1] = cp[1][0];
ye[je][ie + 1] = cp[1][1];
}
// The very first cubic interpolation point (to the left for i = 1 above) is
// used as a *quadratic* interpolation point by the spline drawing function
// which isn't really correct. But for the sake of consistency, we'll use it
// as such. Since we're using cubic splines, that means we need to shorten the
// tangent by 1/3 and also construct a new cubic spline control point 1/3 from
// the original to the i = 0 point.
p1 = inferCubicControlPoint(
[xe[je][0], ye[je][0]],
[xe[je][2], ye[je][2]],
[xe[je][3], ye[je][3]]
);
xe[je][1] = p1[0];
ye[je][1] = p1[1];
// Ditto last points, sans explanation:
p1 = inferCubicControlPoint(
[xe[je][nea - 1], ye[je][nea - 1]],
[xe[je][nea - 3], ye[je][nea - 3]],
[xe[je][nea - 4], ye[je][nea - 4]]
);
xe[je][nea - 2] = p1[0];
ye[je][nea - 2] = p1[1];
}
}
if(bsmoothing) {
// If there's a-smoothing, this loop fills in the X'd points with catmull-rom
// control points computed along the b-axis:
// . . . .
// X X X X X X X X X X
// | | | |
// X X X X X X X X X X
// o -o-o- o -o-o- o -o-o- o
// X X X X X X X X X X
// | | | |
// Y Y Y Y Y Y Y Y Y Y
// o -o-o- o -o-o- o -o-o- o
//
// i: 0 1 2 3
// ie: 0 1 3 3 4 5 6 7 8 9
//
// ------>
// a
//
for(ie = 0; ie < nea; ie++) {
for(je = 3; je < neb - 3; je += 3) {
cp = makeControlPoints(
[xe[je - 3][ie], ye[je - 3][ie]],
[xe[je][ie], ye[je][ie]],
[xe[je + 3][ie], ye[je + 3][ie]],
bsmoothing
);
xe[je - 1][ie] = cp[0][0];
ye[je - 1][ie] = cp[0][1];
xe[je + 1][ie] = cp[1][0];
ye[je + 1][ie] = cp[1][1];
}
// Do the same boundary condition magic for these control points marked Y above:
p1 = inferCubicControlPoint(
[xe[0][ie], ye[0][ie]],
[xe[2][ie], ye[2][ie]],
[xe[3][ie], ye[3][ie]]
);
xe[1][ie] = p1[0];
ye[1][ie] = p1[1];
p1 = inferCubicControlPoint(
[xe[neb - 1][ie], ye[neb - 1][ie]],
[xe[neb - 3][ie], ye[neb - 3][ie]],
[xe[neb - 4][ie], ye[neb - 4][ie]]
);
xe[neb - 2][ie] = p1[0];
ye[neb - 2][ie] = p1[1];
}
}
if(asmoothing && bsmoothing) {
// Do one more pass, this time recomputing exactly what we just computed.
// It's overdetermined since we're peforming catmull-rom in two directions,
// so we'll just average the overdetermined. These points don't lie along the
// grid lines, so note that only grid lines will follow normal plotly spline
// interpolation.
//
// Unless of course there was no b smoothing. Then these intermediate points
// don't actually exist and this section is bypassed.
// . . . .
// o X X o X X o X X o
// | | | |
// o X X o X X o X X o
// o -o-o- o -o-o- o -o-o- o
// o X X o X X o X X o
// | | | |
// o Y Y o Y Y o Y Y o
// o -o-o- o -o-o- o -o-o- o
//
// i: 0 1 2 3
// ie: 0 1 3 3 4 5 6 7 8 9
//
// ------>
// a
//
for(je = 1; je < neb; je += (je + 1) % 3 === 0 ? 2 : 1) {
// Fill in the points marked X for this a-row:
for(ie = 3; ie < nea - 3; ie += 3) {
cp = makeControlPoints(
[xe[je][ie - 3], ye[je][ie - 3]],
[xe[je][ie], ye[je][ie]],
[xe[je][ie + 3], ye[je][ie + 3]],
asmoothing
);
xe[je][ie - 1] = 0.5 * (xe[je][ie - 1] + cp[0][0]);
ye[je][ie - 1] = 0.5 * (ye[je][ie - 1] + cp[0][1]);
xe[je][ie + 1] = 0.5 * (xe[je][ie + 1] + cp[1][0]);
ye[je][ie + 1] = 0.5 * (ye[je][ie + 1] + cp[1][1]);
}
// This case is just slightly different. The computation is the same,
// but having computed this, we'll average with the existing result.
p1 = inferCubicControlPoint(
[xe[je][0], ye[je][0]],
[xe[je][2], ye[je][2]],
[xe[je][3], ye[je][3]]
);
xe[je][1] = 0.5 * (xe[je][1] + p1[0]);
ye[je][1] = 0.5 * (ye[je][1] + p1[1]);
p1 = inferCubicControlPoint(
[xe[je][nea - 1], ye[je][nea - 1]],
[xe[je][nea - 3], ye[je][nea - 3]],
[xe[je][nea - 4], ye[je][nea - 4]]
);
xe[je][nea - 2] = 0.5 * (xe[je][nea - 2] + p1[0]);
ye[je][nea - 2] = 0.5 * (ye[je][nea - 2] + p1[1]);
}
}
return [xe, ye];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
RELATIVE_CULL_TOLERANCE: 1e-6
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Evaluates the derivative of a list of control point arrays. That is, it expects an array or arrays * that are expanded relative to the raw data to include the bicubic control points, if applicable. If * only linear interpolation is desired, then the data points correspond 1-1 along that axis to the * data itself. Since it's catmull-rom splines in either direction note in particular that the * derivatives are discontinuous across cell boundaries. That's the reason you need both the *cell* * and the *point within the cell*. * * Also note that the discontinuity of the derivative is in magnitude only. The direction *is* * continuous across cell boundaries. * * For example, to compute the derivative of the xcoordinate halfway betwen the 7 and 8th i-gridpoints * and the 10th and 11th j-gridpoints given bicubic smoothing in both dimensions, you'd write: * * var deriv = createIDerivativeEvaluator([x], 1, 1); * * var dxdi = deriv([], 7, 10, 0.5, 0.5); * // => [0.12345] * * Since there'd be a bunch of duplicate computation to compute multiple derivatives, you can double * this up by providing more arrays: * * var deriv = createIDerivativeEvaluator([x, y], 1, 1); * * var dxdi = deriv([], 7, 10, 0.5, 0.5); * // => [0.12345, 0.78910] * * NB: It's presumed that at this point all data has been sanitized and is valid numerical data arrays * of the correct dimension. */ module.exports = function(arrays, asmoothing, bsmoothing) { if(asmoothing && bsmoothing) { return function(out, i0, j0, u, v) { if(!out) out = []; var f0, f1, f2, f3, ak, k; // Since it's a grid of control points, the actual indices are * 3: i0 *= 3; j0 *= 3; // Precompute some numbers: var u2 = u * u; var ou = 1 - u; var ou2 = ou * ou; var ouu2 = ou * u * 2; var a = -3 * ou2; var b = 3 * (ou2 - ouu2); var c = 3 * (ouu2 - u2); var d = 3 * u2; var v2 = v * v; var v3 = v2 * v; var ov = 1 - v; var ov2 = ov * ov; var ov3 = ov2 * ov; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; // Compute the derivatives in the u-direction: f0 = a * ak[j0 ][i0] + b * ak[j0 ][i0 + 1] + c * ak[j0 ][i0 + 2] + d * ak[j0 ][i0 + 3]; f1 = a * ak[j0 + 1][i0] + b * ak[j0 + 1][i0 + 1] + c * ak[j0 + 1][i0 + 2] + d * ak[j0 + 1][i0 + 3]; f2 = a * ak[j0 + 2][i0] + b * ak[j0 + 2][i0 + 1] + c * ak[j0 + 2][i0 + 2] + d * ak[j0 + 2][i0 + 3]; f3 = a * ak[j0 + 3][i0] + b * ak[j0 + 3][i0 + 1] + c * ak[j0 + 3][i0 + 2] + d * ak[j0 + 3][i0 + 3]; // Now just interpolate in the v-direction since it's all separable: out[k] = ov3 * f0 + 3 * (ov2 * v * f1 + ov * v2 * f2) + v3 * f3; } return out; }; } else if(asmoothing) { // Handle smooth in the a-direction but linear in the b-direction by performing four // linear interpolations followed by one cubic interpolation of the result return function(out, i0, j0, u, v) { if(!out) out = []; var f0, f1, k, ak; i0 *= 3; var u2 = u * u; var ou = 1 - u; var ou2 = ou * ou; var ouu2 = ou * u * 2; var a = -3 * ou2; var b = 3 * (ou2 - ouu2); var c = 3 * (ouu2 - u2); var d = 3 * u2; var ov = 1 - v; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = a * ak[j0 ][i0] + b * ak[j0 ][i0 + 1] + c * ak[j0 ][i0 + 2] + d * ak[j0 ][i0 + 3]; f1 = a * ak[j0 + 1][i0] + b * ak[j0 + 1][i0 + 1] + c * ak[j0 + 1][i0 + 2] + d * ak[j0 + 1][i0 + 3]; out[k] = ov * f0 + v * f1; } return out; }; } else if(bsmoothing) { // Same as the above case, except reversed. I've disabled the no-unused vars rule // so that this function is fully interpolation-agnostic. Otherwise it would need // to be called differently in different cases. Which wouldn't be the worst, but /* eslint-disable no-unused-vars */ return function(out, i0, j0, u, v) { /* eslint-enable no-unused-vars */ if(!out) out = []; var f0, f1, f2, f3, k, ak; j0 *= 3; var v2 = v * v; var v3 = v2 * v; var ov = 1 - v; var ov2 = ov * ov; var ov3 = ov2 * ov; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ak[j0][i0 + 1] - ak[j0][i0]; f1 = ak[j0 + 1][i0 + 1] - ak[j0 + 1][i0]; f2 = ak[j0 + 2][i0 + 1] - ak[j0 + 2][i0]; f3 = ak[j0 + 3][i0 + 1] - ak[j0 + 3][i0]; out[k] = ov3 * f0 + 3 * (ov2 * v * f1 + ov * v2 * f2) + v3 * f3; } return out; }; } else { // Finally, both directions are linear: /* eslint-disable no-unused-vars */ return function(out, i0, j0, u, v) { /* eslint-enable no-unused-vars */ if(!out) out = []; var f0, f1, k, ak; var ov = 1 - v; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ak[j0][i0 + 1] - ak[j0][i0]; f1 = ak[j0 + 1][i0 + 1] - ak[j0 + 1][i0]; out[k] = ov * f0 + v * f1; } return out; }; } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function(arrays, asmoothing, bsmoothing) { if(asmoothing && bsmoothing) { return function(out, i0, j0, u, v) { if(!out) out = []; var f0, f1, f2, f3, ak, k; // Since it's a grid of control points, the actual indices are * 3: i0 *= 3; j0 *= 3; // Precompute some numbers: var u2 = u * u; var u3 = u2 * u; var ou = 1 - u; var ou2 = ou * ou; var ou3 = ou2 * ou; var v2 = v * v; var ov = 1 - v; var ov2 = ov * ov; var ovv2 = ov * v * 2; var a = -3 * ov2; var b = 3 * (ov2 - ovv2); var c = 3 * (ovv2 - v2); var d = 3 * v2; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; // Compute the derivatives in the v-direction: f0 = a * ak[j0][i0] + b * ak[j0 + 1][i0] + c * ak[j0 + 2][i0] + d * ak[j0 + 3][i0]; f1 = a * ak[j0][i0 + 1] + b * ak[j0 + 1][i0 + 1] + c * ak[j0 + 2][i0 + 1] + d * ak[j0 + 3][i0 + 1]; f2 = a * ak[j0][i0 + 2] + b * ak[j0 + 1][i0 + 2] + c * ak[j0 + 2][i0 + 2] + d * ak[j0 + 3][i0 + 2]; f3 = a * ak[j0][i0 + 3] + b * ak[j0 + 1][i0 + 3] + c * ak[j0 + 2][i0 + 3] + d * ak[j0 + 3][i0 + 3]; // Now just interpolate in the v-direction since it's all separable: out[k] = ou3 * f0 + 3 * (ou2 * u * f1 + ou * u2 * f2) + u3 * f3; } return out; }; } else if(asmoothing) { // Handle smooth in the a-direction but linear in the b-direction by performing four // linear interpolations followed by one cubic interpolation of the result return function(out, i0, j0, v, u) { if(!out) out = []; var f0, f1, f2, f3, k, ak; i0 *= 3; var u2 = u * u; var u3 = u2 * u; var ou = 1 - u; var ou2 = ou * ou; var ou3 = ou2 * ou; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ak[j0 + 1][i0] - ak[j0][i0]; f1 = ak[j0 + 1][i0 + 1] - ak[j0][i0 + 1]; f2 = ak[j0 + 1][i0 + 2] - ak[j0][i0 + 2]; f3 = ak[j0 + 1][i0 + 3] - ak[j0][i0 + 3]; out[k] = ou3 * f0 + 3 * (ou2 * u * f1 + ou * u2 * f2) + u3 * f3; // mathematically equivalent: // f0 = ou3 * ak[j0 ][i0] + 3 * (ou2 * u * ak[j0 ][i0 + 1] + ou * u2 * ak[j0 ][i0 + 2]) + u3 * ak[j0 ][i0 + 3]; // f1 = ou3 * ak[j0 + 1][i0] + 3 * (ou2 * u * ak[j0 + 1][i0 + 1] + ou * u2 * ak[j0 + 1][i0 + 2]) + u3 * ak[j0 + 1][i0 + 3]; // out[k] = f1 - f0; } return out; }; } else if(bsmoothing) { // Same as the above case, except reversed: /* eslint-disable no-unused-vars */ return function(out, i0, j0, u, v) { /* eslint-enable no-unused-vars */ if(!out) out = []; var f0, f1, k, ak; j0 *= 3; var ou = 1 - u; var v2 = v * v; var ov = 1 - v; var ov2 = ov * ov; var ovv2 = ov * v * 2; var a = -3 * ov2; var b = 3 * (ov2 - ovv2); var c = 3 * (ovv2 - v2); var d = 3 * v2; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = a * ak[j0][i0] + b * ak[j0 + 1][i0] + c * ak[j0 + 2][i0] + d * ak[j0 + 3][i0]; f1 = a * ak[j0][i0 + 1] + b * ak[j0 + 1][i0 + 1] + c * ak[j0 + 2][i0 + 1] + d * ak[j0 + 3][i0 + 1]; out[k] = ou * f0 + u * f1; } return out; }; } else { // Finally, both directions are linear: /* eslint-disable no-unused-vars */ return function(out, i0, j0, v, u) { /* eslint-enable no-unused-vars */ if(!out) out = []; var f0, f1, k, ak; var ov = 1 - v; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ak[j0 + 1][i0] - ak[j0][i0]; f1 = ak[j0 + 1][i0 + 1] - ak[j0][i0 + 1]; out[k] = ov * f0 + v * f1; } return out; }; } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Return a function that evaluates a set of linear or bicubic control points. * This will get evaluated a lot, so we'll at least do a bit of extra work to * flatten some of the choices. In particular, we'll unroll the linear/bicubic * combinations and we'll allow computing results in parallel to cut down * on repeated arithmetic. * * Take note that we don't search for the correct range in this function. The * reason is for consistency due to the corrresponding derivative function. In * particular, the derivatives aren't continuous across cells, so it's important * to be able control whether the derivative at a cell boundary is approached * from one side or the other. */ module.exports = function(arrays, na, nb, asmoothing, bsmoothing) { var imax = na - 2; var jmax = nb - 2; if(asmoothing && bsmoothing) { return function(out, i, j) { if(!out) out = []; var f0, f1, f2, f3, ak, k; var i0 = Math.max(0, Math.min(Math.floor(i), imax)); var j0 = Math.max(0, Math.min(Math.floor(j), jmax)); var u = Math.max(0, Math.min(1, i - i0)); var v = Math.max(0, Math.min(1, j - j0)); // Since it's a grid of control points, the actual indices are * 3: i0 *= 3; j0 *= 3; // Precompute some numbers: var u2 = u * u; var u3 = u2 * u; var ou = 1 - u; var ou2 = ou * ou; var ou3 = ou2 * ou; var v2 = v * v; var v3 = v2 * v; var ov = 1 - v; var ov2 = ov * ov; var ov3 = ov2 * ov; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ou3 * ak[j0][i0] + 3 * (ou2 * u * ak[j0][i0 + 1] + ou * u2 * ak[j0][i0 + 2]) + u3 * ak[j0][i0 + 3]; f1 = ou3 * ak[j0 + 1][i0] + 3 * (ou2 * u * ak[j0 + 1][i0 + 1] + ou * u2 * ak[j0 + 1][i0 + 2]) + u3 * ak[j0 + 1][i0 + 3]; f2 = ou3 * ak[j0 + 2][i0] + 3 * (ou2 * u * ak[j0 + 2][i0 + 1] + ou * u2 * ak[j0 + 2][i0 + 2]) + u3 * ak[j0 + 2][i0 + 3]; f3 = ou3 * ak[j0 + 3][i0] + 3 * (ou2 * u * ak[j0 + 3][i0 + 1] + ou * u2 * ak[j0 + 3][i0 + 2]) + u3 * ak[j0 + 3][i0 + 3]; out[k] = ov3 * f0 + 3 * (ov2 * v * f1 + ov * v2 * f2) + v3 * f3; } return out; }; } else if(asmoothing) { // Handle smooth in the a-direction but linear in the b-direction by performing four // linear interpolations followed by one cubic interpolation of the result return function(out, i, j) { if(!out) out = []; var i0 = Math.max(0, Math.min(Math.floor(i), imax)); var j0 = Math.max(0, Math.min(Math.floor(j), jmax)); var u = Math.max(0, Math.min(1, i - i0)); var v = Math.max(0, Math.min(1, j - j0)); var f0, f1, f2, f3, k, ak; i0 *= 3; var u2 = u * u; var u3 = u2 * u; var ou = 1 - u; var ou2 = ou * ou; var ou3 = ou2 * ou; var ov = 1 - v; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ov * ak[j0][i0] + v * ak[j0 + 1][i0]; f1 = ov * ak[j0][i0 + 1] + v * ak[j0 + 1][i0 + 1]; f2 = ov * ak[j0][i0 + 2] + v * ak[j0 + 1][i0 + 1]; f3 = ov * ak[j0][i0 + 3] + v * ak[j0 + 1][i0 + 1]; out[k] = ou3 * f0 + 3 * (ou2 * u * f1 + ou * u2 * f2) + u3 * f3; } return out; }; } else if(bsmoothing) { // Same as the above case, except reversed: return function(out, i, j) { if(!out) out = []; var i0 = Math.max(0, Math.min(Math.floor(i), imax)); var j0 = Math.max(0, Math.min(Math.floor(j), jmax)); var u = Math.max(0, Math.min(1, i - i0)); var v = Math.max(0, Math.min(1, j - j0)); var f0, f1, f2, f3, k, ak; j0 *= 3; var v2 = v * v; var v3 = v2 * v; var ov = 1 - v; var ov2 = ov * ov; var ov3 = ov2 * ov; var ou = 1 - u; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ou * ak[j0][i0] + u * ak[j0][i0 + 1]; f1 = ou * ak[j0 + 1][i0] + u * ak[j0 + 1][i0 + 1]; f2 = ou * ak[j0 + 2][i0] + u * ak[j0 + 2][i0 + 1]; f3 = ou * ak[j0 + 3][i0] + u * ak[j0 + 3][i0 + 1]; out[k] = ov3 * f0 + 3 * (ov2 * v * f1 + ov * v2 * f2) + v3 * f3; } return out; }; } else { // Finally, both directions are linear: return function(out, i, j) { if(!out) out = []; var i0 = Math.max(0, Math.min(Math.floor(i), imax)); var j0 = Math.max(0, Math.min(Math.floor(j), jmax)); var u = Math.max(0, Math.min(1, i - i0)); var v = Math.max(0, Math.min(1, j - j0)); var f0, f1, k, ak; var ov = 1 - v; var ou = 1 - u; for(k = 0; k < arrays.length; k++) { ak = arrays[k]; f0 = ou * ak[j0][i0] + u * ak[j0][i0 + 1]; f1 = ou * ak[j0 + 1][i0] + u * ak[j0 + 1][i0 + 1]; out[k] = ov * f0 + v * f1; } return out; }; } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var handleXYDefaults = require('./xy_defaults');
var handleABDefaults = require('./ab_defaults');
var setConvert = require('./set_convert');
var attributes = require('./attributes');
var colorAttrs = require('../../components/color/attributes');
module.exports = function supplyDefaults(traceIn, traceOut, dfltColor, fullLayout) {
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}
var defaultColor = coerce('color', colorAttrs.defaultLine);
Lib.coerceFont(coerce, 'font');
coerce('carpet');
handleABDefaults(traceIn, traceOut, fullLayout, coerce, defaultColor);
if(!traceOut.a || !traceOut.b) {
traceOut.visible = false;
return;
}
if(traceOut.a.length < 3) {
traceOut.aaxis.smoothing = 0;
}
if(traceOut.b.length < 3) {
traceOut.baxis.smoothing = 0;
}
// NB: the input is x/y arrays. You should know that the *first* dimension of x and y
// corresponds to b and the second to a. This sounds backwards but ends up making sense
// the important part to know is that when you write y[j][i], j goes from 0 to b.length - 1
// and i goes from 0 to a.length - 1.
var len = handleXYDefaults(traceIn, traceOut, coerce);
setConvert(traceOut);
if(traceOut._cheater) {
coerce('cheaterslope');
}
if(!len) {
traceOut.visible = false;
return;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function(data) { return Array.isArray(data[0]); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Carpet = {};
Carpet.attributes = require('./attributes');
Carpet.supplyDefaults = require('./defaults');
Carpet.plot = require('./plot');
Carpet.calc = require('./calc');
Carpet.animatable = true;
Carpet.moduleType = 'trace';
Carpet.name = 'carpet';
Carpet.basePlotModule = require('../../plots/cartesian');
Carpet.categories = ['cartesian', 'carpet', 'carpetAxis', 'notLegendIsolatable'];
Carpet.meta = {
description: [
'The data describing carpet axis layout is set in `y` and (optionally)',
'also `x`. If only `y` is present, `x` the plot is interpreted as a',
'cheater plot and is filled in using the `y` values.',
'`x` and `y` may either be 2D arrays matching with each dimension matching',
'that of `a` and `b`, or they may be 1D arrays with total length equal to',
'that of `a` and `b`.'
].join(' ')
};
module.exports = Carpet;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Given a trace, look up the carpet axis by carpet. */ module.exports = function(gd, trace) { var n = gd._fullData.length; var firstAxis; for(var i = 0; i < n; i++) { var maybeCarpet = gd._fullData[i]; if(maybeCarpet.index === trace.index) continue; if(maybeCarpet.type === 'carpet') { if(!firstAxis) { firstAxis = maybeCarpet; } if(maybeCarpet.carpet === trace.carpet) { return maybeCarpet; } } } return firstAxis; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function makePath(xp, yp, isBicubic) { // Prevent d3 errors that would result otherwise: if(xp.length === 0) return ''; var i, path = []; var stride = isBicubic ? 3 : 1; for(i = 0; i < xp.length; i += stride) { path.push(xp[i] + ',' + yp[i]); if(isBicubic && i < xp.length - stride) { path.push('C'); path.push([ xp[i + 1] + ',' + yp[i + 1], xp[i + 2] + ',' + yp[i + 2] + ' ', ].join(' ')); } } return path.join(isBicubic ? '' : 'L'); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Map an array of x or y coordinates (c) to screen-space pixel coordinates (p). * The output array is optional, but if provided, it will be reused without * reallocation to the extent possible. */ module.exports = function mapArray(out, data, func) { var i; if(!Array.isArray(out)) { // If not an array, make it an array: out = []; } else if(out.length > data.length) { // If too long, truncate. (If too short, it will grow // automatically so we don't care about that case) out = out.slice(0, data.length); } for(i = 0; i < data.length; i++) { out[i] = func(data[i]); } return out; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Map an array of x or y coordinates (c) to screen-space pixel coordinates (p). * The output array is optional, but if provided, it will be reused without * reallocation to the extent possible. */ module.exports = function mapArray(out, data, func) { var i, j; if(!Array.isArray(out)) { // If not an array, make it an array: out = []; } else if(out.length > data.length) { // If too long, truncate. (If too short, it will grow // automatically so we don't care about that case) out = out.slice(0, data.length); } for(i = 0; i < data.length; i++) { if(!Array.isArray(out[i])) { // If not an array, make it an array: out[i] = []; } else if(out[i].length > data.length) { // If too long, truncate. (If too short, it will grow // automatically so we don't care about[i] that case) out[i] = out[i].slice(0, data.length); } for(j = 0; j < data[0].length; j++) { out[i][j] = func(data[i][j]); } } return out; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function orientText(trace, xaxis, yaxis, xy, dxy, refDxy) { var dx = dxy[0] * trace.dpdx(xaxis); var dy = dxy[1] * trace.dpdy(yaxis); var flip = 1; var offsetMultiplier = 1.0; if(refDxy) { var l1 = Math.sqrt(dxy[0] * dxy[0] + dxy[1] * dxy[1]); var l2 = Math.sqrt(refDxy[0] * refDxy[0] + refDxy[1] * refDxy[1]); var dot = (dxy[0] * refDxy[0] + dxy[1] * refDxy[1]) / l1 / l2; offsetMultiplier = Math.max(0.0, dot); } var angle = Math.atan2(dy, dx) * 180 / Math.PI; if(angle < -90) { angle += 180; flip = -flip; } else if(angle > 90) { angle -= 180; flip = -flip; } return { angle: angle, flip: flip, p: trace.c2p(xy, xaxis, yaxis), offsetMultplier: offsetMultiplier }; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Drawing = require('../../components/drawing');
var map1dArray = require('./map_1d_array');
var makepath = require('./makepath');
var orientText = require('./orient_text');
module.exports = function plot(gd, plotinfo, cdcarpet) {
for(var i = 0; i < cdcarpet.length; i++) {
plotOne(gd, plotinfo, cdcarpet[i]);
}
};
function makeg(el, type, klass) {
var join = el.selectAll(type + '.' + klass).data([0]);
join.enter().append(type).classed(klass, true);
return join;
}
function plotOne(gd, plotinfo, cd) {
var t = cd[0];
var trace = cd[0].trace,
xa = plotinfo.xaxis,
ya = plotinfo.yaxis,
aax = trace.aaxis,
bax = trace.baxis,
fullLayout = gd._fullLayout;
// uid = trace.uid,
// id = 'carpet' + uid;
var gridLayer = plotinfo.plot.selectAll('.carpetlayer');
var clipLayer = makeg(fullLayout._defs, 'g', 'clips');
var axisLayer = makeg(gridLayer, 'g', 'carpet' + trace.uid).classed('trace', true);
var minorLayer = makeg(axisLayer, 'g', 'minorlayer');
var majorLayer = makeg(axisLayer, 'g', 'majorlayer');
var boundaryLayer = makeg(axisLayer, 'g', 'boundarylayer');
var labelLayer = makeg(axisLayer, 'g', 'labellayer');
axisLayer.style('opacity', trace.opacity);
drawGridLines(xa, ya, majorLayer, aax, 'a', aax._gridlines, true);
drawGridLines(xa, ya, majorLayer, bax, 'b', bax._gridlines, true);
drawGridLines(xa, ya, minorLayer, aax, 'a', aax._minorgridlines, true);
drawGridLines(xa, ya, minorLayer, bax, 'b', bax._minorgridlines, true);
// NB: These are not ommitted if the lines are not active. The joins must be executed
// in order for them to get cleaned up without a full redraw
drawGridLines(xa, ya, boundaryLayer, aax, 'a-boundary', aax._boundarylines);
drawGridLines(xa, ya, boundaryLayer, bax, 'b-boundary', bax._boundarylines);
var maxAExtent = drawAxisLabels(gd._tester, xa, ya, trace, t, labelLayer, aax._labels, 'a-label');
var maxBExtent = drawAxisLabels(gd._tester, xa, ya, trace, t, labelLayer, bax._labels, 'b-label');
drawAxisTitles(labelLayer, trace, t, xa, ya, maxAExtent, maxBExtent);
// Swap for debugging in order to draw directly:
// drawClipPath(trace, axisLayer, xa, ya);
drawClipPath(trace, t, clipLayer, xa, ya);
}
function drawClipPath(trace, t, layer, xaxis, yaxis) {
var seg, xp, yp, i;
// var clip = makeg(layer, 'g', 'carpetclip');
trace.clipPathId = 'clip' + trace.uid + 'carpet';
var clip = layer.select('#' + trace.clipPathId);
if(!clip.size()) {
clip = layer.append('clipPath')
.classed('carpetclip', true);
}
var path = makeg(clip, 'path', 'carpetboundary');
var segments = t.clipsegments;
var segs = [];
for(i = 0; i < segments.length; i++) {
seg = segments[i];
xp = map1dArray([], seg.x, xaxis.c2p);
yp = map1dArray([], seg.y, yaxis.c2p);
segs.push(makepath(xp, yp, seg.bicubic));
}
// This could be optimized ever so slightly to avoid no-op L segments
// at the corners, but it's so negligible that I don't think it's worth
// the extra complexity
trace.clipPathData = 'M' + segs.join('L') + 'Z';
clip.attr('id', trace.clipPathId);
path.attr('d', trace.clipPathData);
// .style('stroke-width', 20)
// .style('vector-effect', 'non-scaling-stroke')
// .style('stroke', 'black')
// .style('fill', 'rgba(0, 0, 0, 0.1)');
}
function drawGridLines(xaxis, yaxis, layer, axis, axisLetter, gridlines) {
var lineClass = 'const-' + axisLetter + '-lines';
var gridJoin = layer.selectAll('.' + lineClass).data(gridlines);
gridJoin.enter().append('path')
.classed(lineClass, true)
.style('vector-effect', 'non-scaling-stroke');
gridJoin.each(function(d) {
var gridline = d;
var x = gridline.x;
var y = gridline.y;
var xp = map1dArray([], x, xaxis.c2p);
var yp = map1dArray([], y, yaxis.c2p);
var path = 'M' + makepath(xp, yp, gridline.smoothing);
var el = d3.select(this);
el.attr('d', path)
.style('stroke-width', gridline.width)
.style('stroke', gridline.color)
.style('fill', 'none');
});
gridJoin.exit().remove();
}
function drawAxisLabels(tester, xaxis, yaxis, trace, t, layer, labels, labelClass) {
var labelJoin = layer.selectAll('text.' + labelClass).data(labels);
labelJoin.enter().append('text')
.classed(labelClass, true);
var maxExtent = 0;
labelJoin.each(function(label) {
// Most of the positioning is done in calc_labels. Only the parts that depend upon
// the screen space representation of the x and y axes are here:
var orientation;
if(label.axis.tickangle === 'auto') {
orientation = orientText(trace, xaxis, yaxis, label.xy, label.dxy);
} else {
var angle = (label.axis.tickangle + 180.0) * Math.PI / 180.0;
orientation = orientText(trace, xaxis, yaxis, label.xy, [Math.cos(angle), Math.sin(angle)]);
}
var direction = (label.endAnchor ? -1 : 1) * orientation.flip;
var bbox = Drawing.measureText(tester, label.text, label.font);
d3.select(this)
.attr('text-anchor', direction > 0 ? 'start' : 'end')
.text(label.text)
.attr('transform',
// Translate to the correct point:
'translate(' + orientation.p[0] + ',' + orientation.p[1] + ') ' +
// Rotate to line up with grid line tangent:
'rotate(' + orientation.angle + ')' +
// Adjust the baseline and indentation:
'translate(' + label.axis.labelpadding * direction + ',' + bbox.height * 0.3 + ')'
)
.call(Drawing.font, label.font.family, label.font.size, label.font.color);
maxExtent = Math.max(maxExtent, bbox.width + label.axis.labelpadding);
});
labelJoin.exit().remove();
return maxExtent;
}
function drawAxisTitles(layer, trace, t, xa, ya, maxAExtent, maxBExtent) {
var a, b, xy, dxy;
a = 0.5 * (trace.a[0] + trace.a[trace.a.length - 1]);
b = trace.b[0];
xy = trace.ab2xy(a, b, true);
dxy = trace.dxyda_rough(a, b);
drawAxisTitle(layer, trace, t, xy, dxy, trace.aaxis, xa, ya, maxAExtent, 'a-title');
a = trace.a[0];
b = 0.5 * (trace.b[0] + trace.b[trace.b.length - 1]);
xy = trace.ab2xy(a, b, true);
dxy = trace.dxydb_rough(a, b);
drawAxisTitle(layer, trace, t, xy, dxy, trace.baxis, xa, ya, maxBExtent, 'b-title');
}
function drawAxisTitle(layer, trace, t, xy, dxy, axis, xa, ya, offset, labelClass) {
var data = [];
if(axis.title) data.push(axis.title);
var titleJoin = layer.selectAll('text.' + labelClass).data(data);
titleJoin.enter().append('text')
.classed(labelClass, true);
// There's only one, but we'll do it as a join so it's updated nicely:
titleJoin.each(function() {
var orientation = orientText(trace, xa, ya, xy, dxy);
if(['start', 'both'].indexOf(axis.showticklabels) === -1) {
offset = 0;
}
// In addition to the size of the labels, add on some extra padding:
offset += axis.titlefont.size + axis.titleoffset;
var el = d3.select(this);
el.text(axis.title || '')
.attr('transform',
'translate(' + orientation.p[0] + ',' + orientation.p[1] + ') ' +
'rotate(' + orientation.angle + ') ' +
'translate(0,' + offset + ')'
)
.classed('user-select-none', true)
.attr('text-anchor', 'middle')
.call(Drawing.font, axis.titlefont);
});
titleJoin.exit().remove();
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var constants = require('./constants');
var search = require('../../lib/search').findBin;
var computeControlPoints = require('./compute_control_points');
var createSplineEvaluator = require('./create_spline_evaluator');
var createIDerivativeEvaluator = require('./create_i_derivative_evaluator');
var createJDerivativeEvaluator = require('./create_j_derivative_evaluator');
/*
* Create conversion functions to go from one basis to another. In particular the letter
* abbreviations are:
*
* i: i/j coordinates along the grid. Integer values correspond to data points
* a: real-valued coordinates along the a/b axes
* c: cartesian x-y coordinates
* p: screen-space pixel coordinates
*/
module.exports = function setConvert(trace) {
var a = trace.a;
var b = trace.b;
var na = trace.a.length;
var nb = trace.b.length;
var aax = trace.aaxis;
var bax = trace.baxis;
// Grab the limits once rather than recomputing the bounds for every point
// independently:
var amin = a[0];
var amax = a[na - 1];
var bmin = b[0];
var bmax = b[nb - 1];
var arange = a[a.length - 1] - a[0];
var brange = b[b.length - 1] - b[0];
// Compute the tolerance so that points are visible slightly outside the
// defined carpet axis:
var atol = arange * constants.RELATIVE_CULL_TOLERANCE;
var btol = brange * constants.RELATIVE_CULL_TOLERANCE;
// Expand the limits to include the relative tolerance:
amin -= atol;
amax += atol;
bmin -= btol;
bmax += btol;
trace.isVisible = function(a, b) {
return a > amin && a < amax && b > bmin && b < bmax;
};
trace.isOccluded = function(a, b) {
return a < amin || a > amax || b < bmin || b > bmax;
};
// XXX: ONLY PASSTHRU. ONLY. No, ONLY.
aax.c2p = function(v) { return v; };
bax.c2p = function(v) { return v; };
trace.setScale = function() {
var x = trace.x;
var y = trace.y;
// This is potentially a very expensive step! It does the bulk of the work of constructing
// an expanded basis of control points. Note in particular that it overwrites the existing
// basis without creating a new array since that would potentially thrash the garbage
// collector.
var result = computeControlPoints(trace.xctrl, trace.yctrl, x, y, aax.smoothing, bax.smoothing);
trace.xctrl = result[0];
trace.yctrl = result[1];
// This step is the second step in the process, but it's somewhat simpler. It just unrolls
// some logic since it would be unnecessarily expensive to compute both interpolations
// nearly identically but separately and to include a bunch of linear vs. bicubic logic in
// every single call.
trace.evalxy = createSplineEvaluator([trace.xctrl, trace.yctrl], na, nb, aax.smoothing, bax.smoothing);
trace.dxydi = createIDerivativeEvaluator([trace.xctrl, trace.yctrl], aax.smoothing, bax.smoothing);
trace.dxydj = createJDerivativeEvaluator([trace.xctrl, trace.yctrl], aax.smoothing, bax.smoothing);
};
/*
* Convert from i/j data grid coordinates to a/b values. Note in particular that this
* is *linear* interpolation, even if the data is interpolated bicubically.
*/
trace.i2a = function(i) {
var i0 = Math.max(0, Math.floor(i[0]), na - 2);
var ti = i[0] - i0;
return (1 - ti) * a[i0] + ti * a[i0 + 1];
};
trace.j2b = function(j) {
var j0 = Math.max(0, Math.floor(j[1]), na - 2);
var tj = j[1] - j0;
return (1 - tj) * b[j0] + tj * b[j0 + 1];
};
trace.ij2ab = function(ij) {
return [trace.i2a(ij[0]), trace.j2b(ij[1])];
};
/*
* Convert from a/b coordinates to i/j grid-numbered coordinates. This requires searching
* through the a/b data arrays and assumes they are monotonic, which is presumed to have
* been enforced already.
*/
trace.a2i = function(aval) {
var i0 = Math.max(0, Math.min(search(aval, a), na - 2));
var a0 = a[i0];
var a1 = a[i0 + 1];
return Math.max(0, Math.min(na - 1, i0 + (aval - a0) / (a1 - a0)));
};
trace.b2j = function(bval) {
var j0 = Math.max(0, Math.min(search(bval, b), nb - 2));
var b0 = b[j0];
var b1 = b[j0 + 1];
return Math.max(0, Math.min(nb - 1, j0 + (bval - b0) / (b1 - b0)));
};
trace.ab2ij = function(ab) {
return [trace.a2i(ab[0]), trace.b2j(ab[1])];
};
/*
* Convert from i/j coordinates to x/y caretesian coordinates. This means either bilinear
* or bicubic spline evaluation, but the hard part is already done at this point.
*/
trace.i2c = function(i, j) {
return trace.evalxy([], i, j);
};
trace.ab2xy = function(aval, bval, extrapolate) {
if(!extrapolate && (aval < a[0] || aval > a[na - 1] | bval < b[0] || bval > b[nb - 1])) {
return [false, false];
}
var i = trace.a2i(aval);
var j = trace.b2j(bval);
var pt = trace.evalxy([], i, j);
if(extrapolate) {
// This section uses the boundary derivatives to extrapolate linearly outside
// the defined range. Consider a scatter line with one point inside the carpet
// axis and one point outside. If we don't extrapolate, we can't draw the line
// at all.
var iex = 0;
var jex = 0;
var der = [];
var i0, ti, j0, tj;
if(aval < a[0]) {
i0 = 0;
ti = 0;
iex = (aval - a[0]) / (a[1] - a[0]);
} else if(aval > a[na - 1]) {
i0 = na - 2;
ti = 1;
iex = (aval - a[na - 1]) / (a[na - 1] - a[na - 2]);
} else {
i0 = Math.max(0, Math.min(na - 2, Math.floor(i)));
ti = i - i0;
}
if(bval < b[0]) {
j0 = 0;
tj = 0;
jex = (bval - b[0]) / (b[1] - b[0]);
} else if(bval > b[nb - 1]) {
j0 = nb - 2;
tj = 1;
jex = (bval - b[nb - 1]) / (b[nb - 1] - b[nb - 2]);
} else {
j0 = Math.max(0, Math.min(nb - 2, Math.floor(j)));
tj = j - j0;
}
if(iex) {
trace.dxydi(der, i0, j0, ti, tj);
pt[0] += der[0] * iex;
pt[1] += der[1] * iex;
}
if(jex) {
trace.dxydj(der, i0, j0, ti, tj);
pt[0] += der[0] * jex;
pt[1] += der[1] * jex;
}
}
return pt;
};
trace.c2p = function(xy, xa, ya) {
return [xa.c2p(xy[0]), ya.c2p(xy[1])];
};
trace.p2x = function(p, xa, ya) {
return [xa.p2c(p[0]), ya.p2c(p[1])];
};
trace.dadi = function(i /* , u*/) {
// Right now only a piecewise linear a or b basis is permitted since smoother interpolation
// would cause monotonicity problems. As a retult, u is entirely disregarded in this
// computation, though we'll specify it as a parameter for the sake of completeness and
// future-proofing. It would be possible to use monotonic cubic interpolation, for example.
//
// See: https://en.wikipedia.org/wiki/Monotone_cubic_interpolation
// u = u || 0;
var i0 = Math.max(0, Math.min(a.length - 2, i));
// The step (demoninator) is implicitly 1 since that's the grid spacing.
return a[i0 + 1] - a[i0];
};
trace.dbdj = function(j /* , v*/) {
// See above caveats for dadi which also apply here
var j0 = Math.max(0, Math.min(b.length - 2, j));
// The step (demoninator) is implicitly 1 since that's the grid spacing.
return b[j0 + 1] - b[j0];
};
// Takes: grid cell coordinate (i, j) and fractional grid cell coordinates (u, v)
// Returns: (dx/da, dy/db)
//
// NB: separate grid cell + fractional grid cell coordinate format is due to the discontinuous
// derivative, as described better in create_i_derivative_evaluator.js
trace.dxyda = function(i0, j0, u, v) {
var dxydi = trace.dxydi(null, i0, j0, u, v);
var dadi = trace.dadi(i0, u);
return [dxydi[0] / dadi, dxydi[1] / dadi];
};
trace.dxydb = function(i0, j0, u, v) {
var dxydj = trace.dxydj(null, i0, j0, u, v);
var dbdj = trace.dbdj(j0, v);
return [dxydj[0] / dbdj, dxydj[1] / dbdj];
};
// Sometimes we don't care about precision and all we really want is decent rough
// directions (as is the case with labels). In that case, we can do a very rough finite
// difference and spare having to worry about precise grid coordinates:
trace.dxyda_rough = function(a, b, reldiff) {
var h = arange * (reldiff || 0.1);
var plus = trace.ab2xy(a + h, b, true);
var minus = trace.ab2xy(a - h, b, true);
return [
(plus[0] - minus[0]) * 0.5 / h,
(plus[1] - minus[1]) * 0.5 / h
];
};
trace.dxydb_rough = function(a, b, reldiff) {
var h = brange * (reldiff || 0.1);
var plus = trace.ab2xy(a, b + h, true);
var minus = trace.ab2xy(a, b - h, true);
return [
(plus[0] - minus[0]) * 0.5 / h,
(plus[1] - minus[1]) * 0.5 / h
];
};
trace.dpdx = function(xa) {
return xa._m;
};
trace.dpdy = function(ya) {
return ya._m;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
/*
* Given a 2D array as well as a basis in either direction, this function fills in the
* 2D array using a combination of smoothing and extrapolation. This is rather important
* for carpet plots since it's used for layout so that we can't simply omit or blank out
* points. We need a reasonable guess so that the interpolation puts points somewhere
* even if we were to somehow represent that the data was missing later on.
*
* input:
* - data: 2D array of arrays
* - a: array such that a.length === data[0].length
* - b: array such that b.length === data.length
*/
module.exports = function smoothFill2dArray(data, a, b) {
var i, j, k;
var ip = [];
var jp = [];
// var neighborCnts = [];
var ni = data[0].length;
var nj = data.length;
function avgSurrounding(i, j) {
// As a low-quality start, we can simply average surrounding points (in a not
// non-uniform grid aware manner):
var sum = 0.0;
var val;
var cnt = 0;
if(i > 0 && (val = data[j][i - 1]) !== undefined) {
cnt++;
sum += val;
}
if(i < ni - 1 && (val = data[j][i + 1]) !== undefined) {
cnt++;
sum += val;
}
if(j > 0 && (val = data[j - 1][i]) !== undefined) {
cnt++;
sum += val;
}
if(j < nj - 1 && (val = data[j + 1][i]) !== undefined) {
cnt++;
sum += val;
}
return sum / Math.max(1, cnt);
}
// This loop iterates over all cells. Any cells that are null will be noted and those
// are the only points we will loop over and update via laplace's equation. Points with
// any neighbors will receive the average. If there are no neighboring points, then they
// will be set to zero. Also as we go, track the maximum magnitude so that we can scale
// our tolerance accordingly.
var dmax = 0.0;
for(i = 0; i < ni; i++) {
for(j = 0; j < nj; j++) {
if(data[j][i] === undefined) {
ip.push(i);
jp.push(j);
data[j][i] = avgSurrounding(i, j);
// neighborCnts.push(result.neighbors);
}
dmax = Math.max(dmax, Math.abs(data[j][i]));
}
}
if(!ip.length) return data;
// The tolerance doesn't need to be excessive. It's just for display positioning
var dxp, dxm, dap, dam, dbp, dbm, c, d, diff, reldiff, overrelaxation;
var tol = 1e-5;
var resid = 0;
var itermax = 100;
var iter = 0;
var n = ip.length;
do {
resid = 0;
// Normally we'd loop in two dimensions, but not all points are blank and need
// an update, so we instead loop only over the points that were tabulated above
for(k = 0; k < n; k++) {
i = ip[k];
j = jp[k];
// neighborCnt = neighborCnts[k];
// Track a counter for how many contributions there are. We'll use this counter
// to average at the end, which reduces to laplace's equation with neumann boundary
// conditions on the first derivative (second derivative is zero so that we get
// a nice linear extrapolation at the boundaries).
var boundaryCnt = 0;
var newVal = 0;
var d0, d1, x0, x1, i0, j0;
if(i === 0) {
// If this lies along the i = 0 boundary, extrapolate from the two points
// to the right of this point. Note that the finite differences take into
// account non-uniform grid spacing:
i0 = Math.min(ni - 1, 2);
x0 = a[i0];
x1 = a[1];
d0 = data[j][i0];
d1 = data[j][1];
newVal += d1 + (d1 - d0) * (a[0] - x1) / (x1 - x0);
boundaryCnt++;
} else if(i === ni - 1) {
// If along the high i boundary, extrapolate from the two points to the
// left of this point
i0 = Math.max(0, ni - 3);
x0 = a[i0];
x1 = a[ni - 2];
d0 = data[j][i0];
d1 = data[j][ni - 2];
newVal += d1 + (d1 - d0) * (a[ni - 1] - x1) / (x1 - x0);
boundaryCnt++;
}
if((i === 0 || i === ni - 1) && (j > 0 && j < nj - 1)) {
// If along the min(i) or max(i) boundaries, also smooth vertically as long
// as we're not in a corner. Note that the finite differences used here
// are also aware of nonuniform grid spacing:
dxp = b[j + 1] - b[j];
dxm = b[j] - b[j - 1];
newVal += (dxm * data[j + 1][i] + dxp * data[j - 1][i]) / (dxm + dxp);
boundaryCnt++;
}
if(j === 0) {
// If along the j = 0 boundary, extrpolate this point from the two points
// above it
j0 = Math.min(nj - 1, 2);
x0 = b[j0];
x1 = b[1];
d0 = data[j0][i];
d1 = data[1][i];
newVal += d1 + (d1 - d0) * (b[0] - x1) / (x1 - x0);
boundaryCnt++;
} else if(j === nj - 1) {
// Same for the max j boundary from the cells below it:
j0 = Math.max(0, nj - 3);
x0 = b[j0];
x1 = b[nj - 2];
d0 = data[j0][i];
d1 = data[nj - 2][i];
newVal += d1 + (d1 - d0) * (b[nj - 1] - x1) / (x1 - x0);
boundaryCnt++;
}
if((j === 0 || j === nj - 1) && (i > 0 && i < ni - 1)) {
// Now average points to the left/right as long as not in a corner:
dxp = a[i + 1] - a[i];
dxm = a[i] - a[i - 1];
newVal += (dxm * data[j][i + 1] + dxp * data[j][i - 1]) / (dxm + dxp);
boundaryCnt++;
}
if(!boundaryCnt) {
// If none of the above conditions were triggered, then this is an interior
// point and we can just do a laplace equation update. As above, these differences
// are aware of nonuniform grid spacing:
dap = a[i + 1] - a[i];
dam = a[i] - a[i - 1];
dbp = b[j + 1] - b[j];
dbm = b[j] - b[j - 1];
// These are just some useful constants for the iteration, which is perfectly
// straightforward but a little long to derive from f_xx + f_yy = 0.
c = dap * dam * (dap + dam);
d = dbp * dbm * (dbp + dbm);
newVal = (c * (dbm * data[j + 1][i] + dbp * data[j - 1][i]) +
d * (dam * data[j][i + 1] + dap * data[j][i - 1])) /
(d * (dam + dap) + c * (dbm + dbp));
} else {
// If we did have contributions from the boundary conditions, then average
// the result from the various contributions:
newVal /= boundaryCnt;
}
// Jacobi updates are ridiculously slow to converge, so this approach uses a
// Gauss-seidel iteration which is dramatically faster.
diff = newVal - data[j][i];
reldiff = diff / dmax;
resid += reldiff * reldiff;
// Gauss-Seidel-ish iteration, omega chosen based on heuristics and some
// quick tests.
//
// NB: Don't overrelax the boundarie. Otherwise set an overrelaxation factor
// which is a little low but safely optimal-ish:
overrelaxation = boundaryCnt ? 0 : 0.85;
// If there are four non-null neighbors, then we want a simple average without
// overrelaxation. If all the surrouding points are null, then we want the full
// overrelaxation
//
// Based on experiments, this actually seems to slow down convergence just a bit.
// I'll leave it here for reference in case this needs to be revisited, but
// it seems to work just fine without this.
// if (overrelaxation) overrelaxation *= (4 - neighborCnt) / 4;
data[j][i] += diff * (1 + overrelaxation);
}
resid = Math.sqrt(resid);
} while(iter++ < itermax && resid > tol);
Lib.log('Smoother converged to', resid, 'after', iter, 'iterations');
return data;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * Fill in a 1D array via linear interpolation. This *is* the basis, so we * don't have to scale this by some basis as we do for the 2D version. That * makes this much simpler. Just loop over it and do the best we can to fill * the array. */ module.exports = function smoothFillArray(data) { var i, i0, i1; var n = data.length; for(i = 0; i < n; i++) { if(data[i] !== undefined) { i0 = i; break; } } for(i = n - 1; i >= 0; i--) { if(data[i] !== undefined) { i1 = i; break; } } if(i0 === undefined) { // Fill with zeros and return early; for(i = 0; i < n; i++) { data[i] = 0; } return data; } else if(i0 === i1) { // Only one data point so can't extrapolate. Fill with it and return early: for(i = 0; i < n; i++) { data[i] = data[i0]; } return data; } var iA = i0; var iB; var m, b, dA, dB; // Fill in interior data. When we land on an undefined point, // look ahead until the next defined point and then fill in linearly: for(i = i0; i < i1; i++) { if(data[i] === undefined) { iA = iB = i; while(iB < i1 && data[iB] === undefined) iB++; dA = data[iA - 1]; dB = data[iB]; // Lots of variables, but it's just mx + b: m = (dB - dA) / (iB - iA + 1); b = dA + (1 - iA) * m; // Note that this *does* increment the outer loop counter. Worried a linter // might complain, but it's the whole point in this case: for(i = iA; i < iB; i++) { data[i] = m * i + b; } i = iA = iB; } } // Fill in up to the first data point: if(i0 > 0) { m = data[i0 + 1] - data[i0]; b = data[i0]; for(i = 0; i < i0; i++) { data[i] = m * (i - i0) + b; } } // Fill in after the last data point: if(i1 < n - 1) { m = data[i1] - data[i1 - 1]; b = data[i1]; for(i = i1 + 1; i < n; i++) { data[i] = m * (i - i1) + b; } } return data; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var hasColumns = require('./has_columns');
var convertColumnData = require('../heatmap/convert_column_xyz');
module.exports = function handleXYDefaults(traceIn, traceOut, coerce) {
var cols = [];
var x = coerce('x');
var needsXTransform = x && !hasColumns(x);
if(needsXTransform) cols.push('x');
traceOut._cheater = !x;
var y = coerce('y');
var needsYTransform = y && !hasColumns(y);
if(needsYTransform) cols.push('y');
if(!x && !y) return;
if(cols.length) {
convertColumnData(traceOut, traceOut.aaxis, traceOut.baxis, 'a', 'b', cols);
}
return true;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 14.29% | (1 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (1 / 7) | |
| event_data.js | 25% | (1 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 25% | (1 / 4) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterGeoAttrs = require('../scattergeo/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var plotAttrs = require('../../plots/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var ScatterGeoMarkerLineAttrs = ScatterGeoAttrs.marker.line;
module.exports = extendFlat({}, {
locations: {
valType: 'data_array',
description: [
'Sets the coordinates via location IDs or names.',
'See `locationmode` for more info.'
].join(' ')
},
locationmode: ScatterGeoAttrs.locationmode,
z: {
valType: 'data_array',
description: 'Sets the color values.'
},
text: {
valType: 'data_array',
description: 'Sets the text elements associated with each location.'
},
marker: {
line: {
color: ScatterGeoMarkerLineAttrs.color,
width: extendFlat({}, ScatterGeoMarkerLineAttrs.width, {dflt: 1})
}
},
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['location', 'z', 'text', 'name']
}),
},
colorscaleAttrs,
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function eventData(out, pt) { out.location = pt.location; out.z = pt.z; return out; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Choropleth = {};
Choropleth.attributes = require('./attributes');
Choropleth.supplyDefaults = require('./defaults');
Choropleth.colorbar = require('../heatmap/colorbar');
Choropleth.calc = require('./calc');
Choropleth.plot = require('./plot');
Choropleth.hoverPoints = require('./hover');
Choropleth.eventData = require('./event_data');
Choropleth.moduleType = 'trace';
Choropleth.name = 'choropleth';
Choropleth.basePlotModule = require('../../plots/geo');
Choropleth.categories = ['geo', 'noOpacity'];
Choropleth.meta = {
description: [
'The data that describes the choropleth value-to-color mapping',
'is set in `z`.',
'The geographic locations corresponding to each value in `z`',
'are set in `locations`.'
].join(' ')
};
module.exports = Choropleth;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 12.5% | (1 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 12.5% | (1 / 8) | |
| colorbar.js | 29.41% | (5 / 17) | 0% | (0 / 14) | 0% | (0 / 1) | 29.41% | (5 / 17) | |
| constants.js | 100% | (7 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (7 / 7) | |
| contours_defaults.js | 15.38% | (2 / 13) | 0% | (0 / 8) | 0% | (0 / 1) | 18.18% | (2 / 11) | |
| end_plus.js | 50% | (1 / 2) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (1 / 2) | |
| find_all_paths.js | 6.21% | (9 / 145) | 0% | (0 / 107) | 0% | (0 / 10) | 6.92% | (9 / 130) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) | |
| make_color_map.js | 12.9% | (4 / 31) | 0% | (0 / 20) | 0% | (0 / 1) | 12.9% | (4 / 31) | |
| make_crossings.js | 8.57% | (3 / 35) | 0% | (0 / 38) | 0% | (0 / 2) | 10.34% | (3 / 29) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 6 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var heatmapAttrs = require('../heatmap/attributes');
var scatterAttrs = require('../scatter/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var dash = require('../../components/drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
var scatterLineAttrs = scatterAttrs.line;
module.exports = extendFlat({}, {
z: heatmapAttrs.z,
x: heatmapAttrs.x,
x0: heatmapAttrs.x0,
dx: heatmapAttrs.dx,
y: heatmapAttrs.y,
y0: heatmapAttrs.y0,
dy: heatmapAttrs.dy,
text: heatmapAttrs.text,
transpose: heatmapAttrs.transpose,
xtype: heatmapAttrs.xtype,
ytype: heatmapAttrs.ytype,
connectgaps: heatmapAttrs.connectgaps,
autocontour: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour level attributes are',
'picked by an algorithm.',
'If *true*, the number of contour levels can be set in `ncontours`.',
'If *false*, set the contour level attributes in `contours`.'
].join(' ')
},
ncontours: {
valType: 'integer',
dflt: 15,
min: 1,
role: 'style',
description: [
'Sets the maximum number of contour levels. The actual number',
'of contours will be chosen automatically to be less than or',
'equal to the value of `ncontours`.',
'Has an effect only if `autocontour` is *true* or if',
'`contours.size` is missing.'
].join(' ')
},
contours: {
start: {
valType: 'number',
dflt: null,
role: 'style',
description: [
'Sets the starting contour level value.',
'Must be less than `contours.end`'
].join(' ')
},
end: {
valType: 'number',
dflt: null,
role: 'style',
description: [
'Sets the end contour level value.',
'Must be more than `contours.start`'
].join(' ')
},
size: {
valType: 'number',
dflt: null,
min: 0,
role: 'style',
description: [
'Sets the step between each contour level.',
'Must be positive.'
].join(' ')
},
coloring: {
valType: 'enumerated',
values: ['fill', 'heatmap', 'lines', 'none'],
dflt: 'fill',
role: 'style',
description: [
'Determines the coloring method showing the contour values.',
'If *fill*, coloring is done evenly between each contour level',
'If *heatmap*, a heatmap gradient coloring is applied',
'between each contour level.',
'If *lines*, coloring is done on the contour lines.',
'If *none*, no coloring is applied on this trace.'
].join(' ')
},
showlines: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour lines are drawn.',
'Has only an effect if `contours.coloring` is set to *fill*.'
].join(' ')
}
},
line: {
color: extendFlat({}, scatterLineAttrs.color, {
description: [
'Sets the color of the contour level.',
'Has no if `contours.coloring` is set to *lines*.'
].join(' ')
}),
width: scatterLineAttrs.width,
dash: dash,
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
description: [
'Sets the amount of smoothing for the contour lines,',
'where *0* corresponds to no smoothing.'
].join(' ')
})
}
},
colorscaleAttrs,
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Plots = require('../../plots/plots');
var drawColorbar = require('../../components/colorbar/draw');
var makeColorMap = require('./make_color_map');
var endPlus = require('./end_plus');
module.exports = function colorbar(gd, cd) {
var trace = cd[0].trace,
cbId = 'cb' + trace.uid;
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
if(!trace.showscale) {
Plots.autoMargin(gd, cbId);
return;
}
var cb = drawColorbar(gd, cbId);
cd[0].t.cb = cb;
var contours = trace.contours,
line = trace.line,
cs = contours.size || 1,
coloring = contours.coloring;
var colorMap = makeColorMap(trace, {isColorbar: true});
if(coloring === 'heatmap') {
cb.filllevels({
start: trace.zmin,
end: trace.zmax,
size: (trace.zmax - trace.zmin) / 254
});
}
cb.fillcolor((coloring === 'fill' || coloring === 'heatmap') ? colorMap : '')
.line({
color: coloring === 'lines' ? colorMap : line.color,
width: contours.showlines !== false ? line.width : 0,
dash: line.dash
})
.levels({
start: contours.start,
end: endPlus(contours),
size: cs
})
.options(trace.colorbar)();
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
// some constants to help with marching squares algorithm
// where does the path start for each index?
module.exports.BOTTOMSTART = [1, 9, 13, 104, 713];
module.exports.TOPSTART = [4, 6, 7, 104, 713];
module.exports.LEFTSTART = [8, 12, 14, 208, 1114];
module.exports.RIGHTSTART = [2, 3, 11, 208, 1114];
// which way [dx,dy] do we leave a given index?
// saddles are already disambiguated
module.exports.NEWDELTA = [
null, [-1, 0], [0, -1], [-1, 0],
[1, 0], null, [0, -1], [-1, 0],
[0, 1], [0, 1], null, [0, 1],
[1, 0], [1, 0], [0, -1]
];
// for each saddle, the first index here is used
// for dx||dy<0, the second for dx||dy>0
module.exports.CHOOSESADDLE = {
104: [4, 1],
208: [2, 8],
713: [7, 13],
1114: [11, 14]
};
// after one index has been used for a saddle, which do we
// substitute to be used up later?
module.exports.SADDLEREMAINDER = {1: 4, 2: 8, 4: 1, 7: 13, 8: 2, 11: 14, 13: 7, 14: 11};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var attributes = require('./attributes');
module.exports = function handleContourDefaults(traceIn, traceOut, coerce) {
var contourStart = Lib.coerce2(traceIn, traceOut, attributes, 'contours.start');
var contourEnd = Lib.coerce2(traceIn, traceOut, attributes, 'contours.end');
var missingEnd = (contourStart === false) || (contourEnd === false);
// normally we only need size if autocontour is off. But contour.calc
// pushes its calculated contour size back to the input trace, so for
// things like restyle that can call supplyDefaults without calc
// after the initial draw, we can just reuse the previous calculation
var contourSize = coerce('contours.size');
var autoContour;
if(missingEnd) autoContour = traceOut.autocontour = true;
else autoContour = coerce('autocontour', false);
if(autoContour || !contourSize) coerce('ncontours');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; /* * tiny helper to move the end of the contours a little to prevent * losing the last contour to rounding errors */ module.exports = function endPlus(contours) { return contours.end + contours.size / 1e6; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var constants = require('./constants');
module.exports = function findAllPaths(pathinfo, xtol, ytol) {
var cnt,
startLoc,
i,
pi,
j;
// Default just passes these values through as they were before:
xtol = xtol || 0.01;
ytol = ytol || 0.01;
for(i = 0; i < pathinfo.length; i++) {
pi = pathinfo[i];
for(j = 0; j < pi.starts.length; j++) {
startLoc = pi.starts[j];
makePath(pi, startLoc, 'edge', xtol, ytol);
}
cnt = 0;
while(Object.keys(pi.crossings).length && cnt < 10000) {
cnt++;
startLoc = Object.keys(pi.crossings)[0].split(',').map(Number);
makePath(pi, startLoc, undefined, xtol, ytol);
}
if(cnt === 10000) Lib.log('Infinite loop in contour?');
}
};
function equalPts(pt1, pt2, xtol, ytol) {
return Math.abs(pt1[0] - pt2[0]) < xtol &&
Math.abs(pt1[1] - pt2[1]) < ytol;
}
function ptDist(pt1, pt2) {
var dx = pt1[0] - pt2[0],
dy = pt1[1] - pt2[1];
return Math.sqrt(dx * dx + dy * dy);
}
function makePath(pi, loc, edgeflag, xtol, ytol) {
var startLocStr = loc.join(',');
var locStr = startLocStr;
var mi = pi.crossings[locStr];
var marchStep = startStep(mi, edgeflag, loc);
// start by going backward a half step and finding the crossing point
var pts = [getInterpPx(pi, loc, [-marchStep[0], -marchStep[1]])];
var startStepStr = marchStep.join(',');
var m = pi.z.length;
var n = pi.z[0].length;
var cnt;
// now follow the path
for(cnt = 0; cnt < 10000; cnt++) { // just to avoid infinite loops
if(mi > 20) {
mi = constants.CHOOSESADDLE[mi][(marchStep[0] || marchStep[1]) < 0 ? 0 : 1];
pi.crossings[locStr] = constants.SADDLEREMAINDER[mi];
}
else {
delete pi.crossings[locStr];
}
marchStep = constants.NEWDELTA[mi];
if(!marchStep) {
Lib.log('Found bad marching index:', mi, loc, pi.level);
break;
}
// find the crossing a half step forward, and then take the full step
pts.push(getInterpPx(pi, loc, marchStep));
loc[0] += marchStep[0];
loc[1] += marchStep[1];
// don't include the same point multiple times
if(equalPts(pts[pts.length - 1], pts[pts.length - 2], xtol, ytol)) pts.pop();
locStr = loc.join(',');
var atEdge = (marchStep[0] && (loc[0] < 0 || loc[0] > n - 2)) ||
(marchStep[1] && (loc[1] < 0 || loc[1] > m - 2)),
closedLoop = (locStr === startLocStr) && (marchStep.join(',') === startStepStr);
// have we completed a loop, or reached an edge?
if((closedLoop) || (edgeflag && atEdge)) break;
mi = pi.crossings[locStr];
}
if(cnt === 10000) {
Lib.log('Infinite loop in contour?');
}
var closedpath = equalPts(pts[0], pts[pts.length - 1], xtol, ytol),
totaldist = 0,
distThresholdFactor = 0.2 * pi.smoothing,
alldists = [],
cropstart = 0,
distgroup,
cnt2,
cnt3,
newpt,
ptcnt,
ptavg,
thisdist;
// check for points that are too close together (<1/5 the average dist,
// less if less smoothed) and just take the center (or avg of center 2)
// this cuts down on funny behavior when a point is very close to a contour level
for(cnt = 1; cnt < pts.length; cnt++) {
thisdist = ptDist(pts[cnt], pts[cnt - 1]);
totaldist += thisdist;
alldists.push(thisdist);
}
var distThreshold = totaldist / alldists.length * distThresholdFactor;
function getpt(i) { return pts[i % pts.length]; }
for(cnt = pts.length - 2; cnt >= cropstart; cnt--) {
distgroup = alldists[cnt];
if(distgroup < distThreshold) {
cnt3 = 0;
for(cnt2 = cnt - 1; cnt2 >= cropstart; cnt2--) {
if(distgroup + alldists[cnt2] < distThreshold) {
distgroup += alldists[cnt2];
}
else break;
}
// closed path with close points wrapping around the boundary?
if(closedpath && cnt === pts.length - 2) {
for(cnt3 = 0; cnt3 < cnt2; cnt3++) {
if(distgroup + alldists[cnt3] < distThreshold) {
distgroup += alldists[cnt3];
}
else break;
}
}
ptcnt = cnt - cnt2 + cnt3 + 1;
ptavg = Math.floor((cnt + cnt2 + cnt3 + 2) / 2);
// either endpoint included: keep the endpoint
if(!closedpath && cnt === pts.length - 2) newpt = pts[pts.length - 1];
else if(!closedpath && cnt2 === -1) newpt = pts[0];
// odd # of points - just take the central one
else if(ptcnt % 2) newpt = getpt(ptavg);
// even # of pts - average central two
else {
newpt = [(getpt(ptavg)[0] + getpt(ptavg + 1)[0]) / 2,
(getpt(ptavg)[1] + getpt(ptavg + 1)[1]) / 2];
}
pts.splice(cnt2 + 1, cnt - cnt2 + 1, newpt);
cnt = cnt2 + 1;
if(cnt3) cropstart = cnt3;
if(closedpath) {
if(cnt === pts.length - 2) pts[cnt3] = pts[pts.length - 1];
else if(cnt === 0) pts[pts.length - 1] = pts[0];
}
}
}
pts.splice(0, cropstart);
// don't return single-point paths (ie all points were the same
// so they got deleted?)
if(pts.length < 2) return;
else if(closedpath) {
pts.pop();
pi.paths.push(pts);
}
else {
if(!edgeflag) {
Lib.log('Unclosed interior contour?',
pi.level, startLocStr, pts.join('L'));
}
// edge path - does it start where an existing edge path ends, or vice versa?
var merged = false;
pi.edgepaths.forEach(function(edgepath, edgei) {
if(!merged && equalPts(edgepath[0], pts[pts.length - 1], xtol, ytol)) {
pts.pop();
merged = true;
// now does it ALSO meet the end of another (or the same) path?
var doublemerged = false;
pi.edgepaths.forEach(function(edgepath2, edgei2) {
if(!doublemerged && equalPts(
edgepath2[edgepath2.length - 1], pts[0], xtol, ytol)) {
doublemerged = true;
pts.splice(0, 1);
pi.edgepaths.splice(edgei, 1);
if(edgei2 === edgei) {
// the path is now closed
pi.paths.push(pts.concat(edgepath2));
}
else {
pi.edgepaths[edgei2] =
pi.edgepaths[edgei2].concat(pts, edgepath2);
}
}
});
if(!doublemerged) {
pi.edgepaths[edgei] = pts.concat(edgepath);
}
}
});
pi.edgepaths.forEach(function(edgepath, edgei) {
if(!merged && equalPts(edgepath[edgepath.length - 1], pts[0], xtol, ytol)) {
pts.splice(0, 1);
pi.edgepaths[edgei] = edgepath.concat(pts);
merged = true;
}
});
if(!merged) pi.edgepaths.push(pts);
}
}
// special function to get the marching step of the
// first point in the path (leading to loc)
function startStep(mi, edgeflag, loc) {
var dx = 0,
dy = 0;
if(mi > 20 && edgeflag) {
// these saddles start at +/- x
if(mi === 208 || mi === 1114) {
// if we're starting at the left side, we must be going right
dx = loc[0] === 0 ? 1 : -1;
}
else {
// if we're starting at the bottom, we must be going up
dy = loc[1] === 0 ? 1 : -1;
}
}
else if(constants.BOTTOMSTART.indexOf(mi) !== -1) dy = 1;
else if(constants.LEFTSTART.indexOf(mi) !== -1) dx = 1;
else if(constants.TOPSTART.indexOf(mi) !== -1) dy = -1;
else dx = -1;
return [dx, dy];
}
function getInterpPx(pi, loc, step) {
var locx = loc[0] + Math.max(step[0], 0),
locy = loc[1] + Math.max(step[1], 0),
zxy = pi.z[locy][locx],
xa = pi.xaxis,
ya = pi.yaxis;
if(step[1]) {
var dx = (pi.level - zxy) / (pi.z[locy][locx + 1] - zxy);
return [xa.c2p((1 - dx) * pi.x[locx] + dx * pi.x[locx + 1], true),
ya.c2p(pi.y[locy], true)];
}
else {
var dy = (pi.level - zxy) / (pi.z[locy + 1][locx] - zxy);
return [xa.c2p(pi.x[locx], true),
ya.c2p((1 - dy) * pi.y[locy] + dy * pi.y[locy + 1], true)];
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Contour = {};
Contour.attributes = require('./attributes');
Contour.supplyDefaults = require('./defaults');
Contour.calc = require('./calc');
Contour.plot = require('./plot');
Contour.style = require('./style');
Contour.colorbar = require('./colorbar');
Contour.hoverPoints = require('./hover');
Contour.moduleType = 'trace';
Contour.name = 'contour';
Contour.basePlotModule = require('../../plots/cartesian');
Contour.categories = ['cartesian', '2dMap', 'contour'];
Contour.meta = {
description: [
'The data from which contour lines are computed is set in `z`.',
'Data in `z` must be a {2D array} of numbers.',
'Say that `z` has N rows and M columns, then by default,',
'these N rows correspond to N y coordinates',
'(set in `y` or auto-generated) and the M columns',
'correspond to M x coordinates (set in `x` or auto-generated).',
'By setting `transpose` to *true*, the above behavior is flipped.'
].join(' ')
};
module.exports = Contour;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Colorscale = require('../../components/colorscale');
var endPlus = require('./end_plus');
module.exports = function makeColorMap(trace) {
var contours = trace.contours,
start = contours.start,
end = endPlus(contours),
cs = contours.size || 1,
nc = Math.floor((end - start) / cs) + 1,
extra = contours.coloring === 'lines' ? 0 : 1;
if(!isFinite(cs)) {
cs = 1;
nc = 1;
}
var scl = trace.colorscale,
len = scl.length;
var domain = new Array(len),
range = new Array(len);
var si, i;
if(contours.coloring === 'heatmap') {
if(trace.zauto && trace.autocontour === false) {
trace.zmin = start - cs / 2;
trace.zmax = trace.zmin + nc * cs;
}
for(i = 0; i < len; i++) {
si = scl[i];
domain[i] = si[0] * (trace.zmax - trace.zmin) + trace.zmin;
range[i] = si[1];
}
// do the contours extend beyond the colorscale?
// if so, extend the colorscale with constants
var zRange = d3.extent([trace.zmin, trace.zmax, contours.start,
contours.start + cs * (nc - 1)]),
zmin = zRange[trace.zmin < trace.zmax ? 0 : 1],
zmax = zRange[trace.zmin < trace.zmax ? 1 : 0];
if(zmin !== trace.zmin) {
domain.splice(0, 0, zmin);
range.splice(0, 0, Range[0]);
}
if(zmax !== trace.zmax) {
domain.push(zmax);
range.push(range[range.length - 1]);
}
}
else {
for(i = 0; i < len; i++) {
si = scl[i];
domain[i] = (si[0] * (nc + extra - 1) - (extra / 2)) * cs + start;
range[i] = si[1];
}
}
return Colorscale.makeColorScaleFunc({
domain: domain,
range: range,
}, {
noNumericCheck: true
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var constants = require('./constants');
// Calculate all the marching indices, for ALL levels at once.
// since we want to be exhaustive we'll check for contour crossings
// at every intersection, rather than just following a path
// TODO: shorten the inner loop to only the relevant levels
module.exports = function makeCrossings(pathinfo) {
var z = pathinfo[0].z,
m = z.length,
n = z[0].length, // we already made sure z isn't ragged in interp2d
twoWide = m === 2 || n === 2,
xi,
yi,
startIndices,
ystartIndices,
label,
corners,
mi,
pi,
i;
for(yi = 0; yi < m - 1; yi++) {
ystartIndices = [];
if(yi === 0) ystartIndices = ystartIndices.concat(constants.BOTTOMSTART);
if(yi === m - 2) ystartIndices = ystartIndices.concat(constants.TOPSTART);
for(xi = 0; xi < n - 1; xi++) {
startIndices = ystartIndices.slice();
if(xi === 0) startIndices = startIndices.concat(constants.LEFTSTART);
if(xi === n - 2) startIndices = startIndices.concat(constants.RIGHTSTART);
label = xi + ',' + yi;
corners = [[z[yi][xi], z[yi][xi + 1]],
[z[yi + 1][xi], z[yi + 1][xi + 1]]];
for(i = 0; i < pathinfo.length; i++) {
pi = pathinfo[i];
mi = getMarchingIndex(pi.level, corners);
if(!mi) continue;
pi.crossings[label] = mi;
if(startIndices.indexOf(mi) !== -1) {
pi.starts.push([xi, yi]);
if(twoWide && startIndices.indexOf(mi,
startIndices.indexOf(mi) + 1) !== -1) {
// the same square has starts from opposite sides
// it's not possible to have starts on opposite edges
// of a corner, only a start and an end...
// but if the array is only two points wide (either way)
// you can have starts on opposite sides.
pi.starts.push([xi, yi]);
}
}
}
}
}
};
// modified marching squares algorithm,
// so we disambiguate the saddle points from the start
// and we ignore the cases with no crossings
// the index I'm using is based on:
// http://en.wikipedia.org/wiki/Marching_squares
// except that the saddles bifurcate and I represent them
// as the decimal combination of the two appropriate
// non-saddle indices
function getMarchingIndex(val, corners) {
var mi = (corners[0][0] > val ? 0 : 1) +
(corners[0][1] > val ? 0 : 2) +
(corners[1][1] > val ? 0 : 4) +
(corners[1][0] > val ? 0 : 8);
if(mi === 5 || mi === 10) {
var avg = (corners[0][0] + corners[0][1] +
corners[1][0] + corners[1][1]) / 4;
// two peaks with a big valley
if(val > avg) return (mi === 5) ? 713 : 1114;
// two valleys with a big ridge
return (mi === 5) ? 104 : 208;
}
return (mi === 15) ? 0 : mi;
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 12.5% | (1 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 12.5% | (1 / 8) | |
| close_boundaries.js | 2.78% | (1 / 36) | 0% | (0 / 20) | 0% | (0 / 1) | 2.78% | (1 / 36) | |
| constants.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| constraint_mapping.js | 56.76% | (21 / 37) | 0% | (0 / 14) | 33.33% | (2 / 6) | 56.76% | (21 / 37) | |
| constraint_value_defaults.js | 10.71% | (3 / 28) | 0% | (0 / 16) | 0% | (0 / 1) | 10.71% | (3 / 28) | |
| convert_to_constraints.js | 5.71% | (2 / 35) | 0% | (0 / 14) | 0% | (0 / 3) | 6.06% | (2 / 33) | |
| empty_pathinfo.js | 18.18% | (2 / 11) | 0% | (0 / 2) | 0% | (0 / 1) | 18.18% | (2 / 11) | |
| index.js | 15.38% | (2 / 13) | 100% | (0 / 0) | 100% | (0 / 0) | 15.38% | (2 / 13) | |
| join_all_paths.js | 10.71% | (9 / 84) | 0% | (0 / 42) | 0% | (0 / 7) | 11.54% | (9 / 78) | |
| map_pathinfo.js | 5.56% | (1 / 18) | 100% | (0 / 0) | 0% | (0 / 1) | 5.56% | (1 / 18) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var heatmapAttrs = require('../heatmap/attributes');
var scatterAttrs = require('../scatter/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var scatterLineAttrs = scatterAttrs.line;
var constants = require('./constants');
module.exports = extendFlat({}, {
carpet: {
valType: 'string',
role: 'info',
description: [
'The `carpet` of the carpet axes on which this contour trace lies'
].join(' ')
},
z: heatmapAttrs.z,
a: heatmapAttrs.x,
a0: heatmapAttrs.x0,
da: heatmapAttrs.dx,
b: heatmapAttrs.y,
b0: heatmapAttrs.y0,
db: heatmapAttrs.dy,
text: heatmapAttrs.text,
transpose: heatmapAttrs.transpose,
atype: heatmapAttrs.xtype,
btype: heatmapAttrs.ytype,
mode: {
valType: 'flaglist',
flags: ['lines', 'fill'],
extras: ['none'],
role: 'info',
description: ['The mode.'].join(' ')
},
connectgaps: heatmapAttrs.connectgaps,
fillcolor: {
valType: 'color',
role: 'style',
description: [
'Sets the fill color.',
'Defaults to a half-transparent variant of the line color,',
'marker color, or marker line color, whichever is available.'
].join(' ')
},
autocontour: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour level attributes are',
'picked by an algorithm.',
'If *true*, the number of contour levels can be set in `ncontours`.',
'If *false*, set the contour level attributes in `contours`.'
].join(' ')
},
ncontours: {
valType: 'integer',
dflt: 15,
min: 1,
role: 'style',
description: [
'Sets the maximum number of contour levels. The actual number',
'of contours will be chosen automatically to be less than or',
'equal to the value of `ncontours`.',
'Has an effect only if `autocontour` is *true* or if',
'`contours.size` is missing.'
].join(' ')
},
contours: {
type: {
valType: 'enumerated',
values: ['levels', 'constraint'],
dflt: 'levels',
role: 'info',
description: [
'If `levels`, the data is represented as a contour plot with multiple',
'levels displayed. If `constraint`, the data is represented as constraints',
'with the invalid region shaded as specified by the `operation` and',
'`value` parameters.'
].join(' ')
},
start: {
valType: 'number',
dflt: null,
role: 'style',
description: [
'Sets the starting contour level value.',
'Must be less than `contours.end`'
].join(' ')
},
end: {
valType: 'number',
dflt: null,
role: 'style',
description: [
'Sets the end contour level value.',
'Must be more than `contours.start`'
].join(' ')
},
size: {
valType: 'number',
dflt: null,
min: 0,
role: 'style',
description: [
'Sets the step between each contour level.',
'Must be positive.'
].join(' ')
},
coloring: {
valType: 'enumerated',
values: ['fill', 'lines', 'none'],
dflt: 'fill',
role: 'style',
description: [
'Determines the coloring method showing the contour values.',
'If *fill*, coloring is done evenly between each contour level',
'If *lines*, coloring is done on the contour lines.',
'If *none*, no coloring is applied on this trace.'
].join(' ')
},
showlines: {
valType: 'boolean',
dflt: true,
role: 'style',
description: [
'Determines whether or not the contour lines are drawn.',
'Has only an effect if `contours.coloring` is set to *fill*.'
].join(' ')
},
operation: {
valType: 'enumerated',
values: [].concat(constants.INEQUALITY_OPS).concat(constants.INTERVAL_OPS).concat(constants.SET_OPS),
role: 'info',
dflt: '=',
description: [
'Sets the filter operation.',
'*=* keeps items equal to `value`',
'*<* keeps items less than `value`',
'*<=* keeps items less than or equal to `value`',
'*>* keeps items greater than `value`',
'*>=* keeps items greater than or equal to `value`',
'*[]* keeps items inside `value[0]` to value[1]` including both bounds`',
'*()* keeps items inside `value[0]` to value[1]` excluding both bounds`',
'*[)* keeps items inside `value[0]` to value[1]` including `value[0]` but excluding `value[1]',
'*(]* keeps items inside `value[0]` to value[1]` excluding `value[0]` but including `value[1]',
'*][* keeps items outside `value[0]` to value[1]` and equal to both bounds`',
'*)(* keeps items outside `value[0]` to value[1]`',
'*](* keeps items outside `value[0]` to value[1]` and equal to `value[0]`',
'*)[* keeps items outside `value[0]` to value[1]` and equal to `value[1]`'
].join(' ')
},
value: {
valType: 'any',
dflt: 0,
role: 'info',
description: [
'Sets the value or values by which to filter by.',
'Values are expected to be in the same type as the data linked',
'to *target*.',
'When `operation` is set to one of the inequality values',
'(' + constants.INEQUALITY_OPS + ')',
'*value* is expected to be a number or a string.',
'When `operation` is set to one of the interval value',
'(' + constants.INTERVAL_OPS + ')',
'*value* is expected to be 2-item array where the first item',
'is the lower bound and the second item is the upper bound.',
'When `operation`, is set to one of the set value',
'(' + constants.SET_OPS + ')',
'*value* is expected to be an array with as many items as',
'the desired set elements.'
].join(' ')
}
},
line: {
color: extendFlat({}, scatterLineAttrs.color, {
description: [
'Sets the color of the contour level.',
'Has no if `contours.coloring` is set to *lines*.'
].join(' ')
}),
width: scatterLineAttrs.width,
dash: scatterLineAttrs.dash,
smoothing: extendFlat({}, scatterLineAttrs.smoothing, {
description: [
'Sets the amount of smoothing for the contour lines,',
'where *0* corresponds to no smoothing.'
].join(' ')
})
}
},
colorscaleAttrs,
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function(pathinfo, operation, perimeter, trace) { // Abandon all hope, ye who enter here. var i, v1, v2; var na = trace.a.length; var nb = trace.b.length; var z = trace.z; var boundaryMax = -Infinity; var boundaryMin = Infinity; for(i = 0; i < nb; i++) { boundaryMin = Math.min(boundaryMin, z[i][0]); boundaryMin = Math.min(boundaryMin, z[i][na - 1]); boundaryMax = Math.max(boundaryMax, z[i][0]); boundaryMax = Math.max(boundaryMax, z[i][na - 1]); } for(i = 1; i < na - 1; i++) { boundaryMin = Math.min(boundaryMin, z[0][i]); boundaryMin = Math.min(boundaryMin, z[nb - 1][i]); boundaryMax = Math.max(boundaryMax, z[0][i]); boundaryMax = Math.max(boundaryMax, z[nb - 1][i]); } switch(operation) { case '>': case '>=': if(trace.contours.value > boundaryMax) { pathinfo[0].prefixBoundary = true; } break; case '<': case '<=': if(trace.contours.value < boundaryMin) { pathinfo[0].prefixBoundary = true; } break; case '[]': case '()': v1 = Math.min.apply(null, trace.contours.value); v2 = Math.max.apply(null, trace.contours.value); if(v2 < boundaryMin) { pathinfo[0].prefixBoundary = true; } if(v1 > boundaryMax) { pathinfo[0].prefixBoundary = true; } break; case '][': case ')(': v1 = Math.min.apply(null, trace.contours.value); v2 = Math.max.apply(null, trace.contours.value); if(v1 < boundaryMin && v2 > boundaryMax) { pathinfo[0].prefixBoundary = true; } break; } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
INEQUALITY_OPS: ['=', '<', '>=', '>', '<='],
INTERVAL_OPS: ['[]', '()', '[)', '(]', '][', ')(', '](', ')['],
SET_OPS: ['{}', '}{']
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 1 5 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var constants = require('./constants');
var isNumeric = require('fast-isnumeric');
// This syntax conforms to the existing filter transform syntax, but we don't care
// about open vs. closed intervals for simply drawing contours constraints:
module.exports['[]'] = makeRangeSettings('[]');
module.exports['()'] = makeRangeSettings('()');
module.exports['[)'] = makeRangeSettings('[)');
module.exports['(]'] = makeRangeSettings('(]');
// Inverted intervals simply flip the sign:
module.exports[']['] = makeRangeSettings('][');
module.exports[')('] = makeRangeSettings(')(');
module.exports[')['] = makeRangeSettings(')[');
module.exports[']('] = makeRangeSettings('](');
module.exports['>'] = makeInequalitySettings('>');
module.exports['>='] = makeInequalitySettings('>=');
module.exports['<'] = makeInequalitySettings('<');
module.exports['<='] = makeInequalitySettings('<=');
module.exports['='] = makeInequalitySettings('=');
// This does not in any way shape or form support calendars. It's adapted from
// transforms/filter.js.
function coerceValue(operation, value) {
var hasArrayValue = Array.isArray(value);
var coercedValue;
function coerce(value) {
return isNumeric(value) ? (+value) : null;
}
if(constants.INEQUALITY_OPS.indexOf(operation) !== -1) {
coercedValue = hasArrayValue ? coerce(value[0]) : coerce(value);
} else if(constants.INTERVAL_OPS.indexOf(operation) !== -1) {
coercedValue = hasArrayValue ?
[coerce(value[0]), coerce(value[1])] :
[coerce(value), coerce(value)];
} else if(constants.SET_OPS.indexOf(operation) !== -1) {
coercedValue = hasArrayValue ? value.map(coerce) : [coerce(value)];
}
return coercedValue;
}
// Returns a parabola scaled so that the min/max is either +/- 1 and zero at the two values
// provided. The data is mapped by this function when constructing intervals so that it's
// very easy to construct contours as normal.
function makeRangeSettings(operation) {
return function(value) {
value = coerceValue(operation, value);
// Ensure proper ordering:
var min = Math.min(value[0], value[1]);
var max = Math.max(value[0], value[1]);
return {
start: min,
end: max,
size: max - min
};
};
}
function makeInequalitySettings(operation) {
return function(value) {
value = coerceValue(operation, value);
return {
start: value,
end: Infinity,
size: Infinity
};
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var constraintMapping = require('./constraint_mapping');
var isNumeric = require('fast-isnumeric');
module.exports = function(coerce, contours) {
var zvalue;
var scalarValuedOps = ['=', '<', '<=', '>', '>='];
if(scalarValuedOps.indexOf(contours.operation) === -1) {
// Requires an array of two numbers:
coerce('contours.value', [0, 1]);
if(!Array.isArray(contours.value)) {
if(isNumeric(contours.value)) {
zvalue = parseFloat(contours.value);
contours.value = [zvalue, zvalue + 1];
}
} else if(contours.value.length > 2) {
contours.value = contours.value.slice(2);
} else if(contours.length === 0) {
contours.value = [0, 1];
} else if(contours.length < 2) {
zvalue = parseFloat(contours.value[0]);
contours.value = [zvalue, zvalue + 1];
} else {
contours.value = [
parseFloat(contours.value[0]),
parseFloat(contours.value[1])
];
}
} else {
// Requires a single scalar:
coerce('contours.value', 0);
if(!isNumeric(contours.value)) {
if(Array.isArray(contours.value)) {
contours.value = parseFloat(contours.value[0]);
} else {
contours.value = 0;
}
}
}
var map = constraintMapping[contours.operation](contours.value);
contours.start = map.start;
contours.end = map.end;
contours.size = map.size;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
// The contour extraction is great, except it totally fails for constraints because we
// need weird range loops and flipped contours instead of the usual format. This function
// does some weird manipulation of the extracted pathinfo data such that it magically
// draws contours correctly *as* constraints.
module.exports = function(pathinfo, operation) {
var i, pi0, pi1;
var op0 = function(arr) { return arr.reverse(); };
var op1 = function(arr) { return arr; };
switch(operation) {
case '][':
case ')[':
case '](':
case ')(':
var tmp = op0;
op0 = op1;
op1 = tmp;
// It's a nice rule, except this definitely *is* what's intended here.
/* eslint-disable: no-fallthrough */
case '[]':
case '[)':
case '(]':
case '()':
/* eslint-enable: no-fallthrough */
if(pathinfo.length !== 2) {
Lib.warn('Contour data invalid for the specified inequality range operation.');
return;
}
// In this case there should be exactly two contour levels in pathinfo. We
// simply concatenate the info into one pathinfo and flip all of the data
// in one. This will draw the contour as closed.
pi0 = pathinfo[0];
pi1 = pathinfo[1];
for(i = 0; i < pi0.edgepaths.length; i++) {
pi0.edgepaths[i] = op0(pi0.edgepaths[i]);
}
for(i = 0; i < pi0.paths.length; i++) {
pi0.paths[i] = op0(pi0.paths[i]);
}
while(pi1.edgepaths.length) {
pi0.edgepaths.push(op1(pi1.edgepaths.shift()));
}
while(pi1.paths.length) {
pi0.paths.push(op1(pi1.paths.shift()));
}
pathinfo.pop();
break;
case '>=':
case '>':
if(pathinfo.length !== 1) {
Lib.warn('Contour data invalid for the specified inequality operation.');
return;
}
// In this case there should be exactly two contour levels in pathinfo. We
// simply concatenate the info into one pathinfo and flip all of the data
// in one. This will draw the contour as closed.
pi0 = pathinfo[0];
for(i = 0; i < pi0.edgepaths.length; i++) {
pi0.edgepaths[i] = op0(pi0.edgepaths[i]);
}
for(i = 0; i < pi0.paths.length; i++) {
pi0.paths[i] = op0(pi0.paths[i]);
}
break;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
module.exports = function emptyPathinfo(contours, plotinfo, cd0) {
var cs = contours.size;
var pathinfo = [];
var carpet = cd0.trace.carpetTrace;
for(var ci = contours.start; ci < contours.end + cs / 10; ci += cs) {
pathinfo.push({
level: ci,
// all the cells with nontrivial marching index
crossings: {},
// starting points on the edges of the lattice for each contour
starts: [],
// all unclosed paths (may have less items than starts,
// if a path is closed by rounding)
edgepaths: [],
// all closed paths
paths: [],
// store axes so we can convert to px
xaxis: carpet.aaxis,
yaxis: carpet.baxis,
// full data arrays to use for interpolation
x: cd0.a,
y: cd0.b,
z: cd0.z,
smoothing: cd0.trace.line.smoothing
});
if(pathinfo.length > 1000) {
Lib.warn('Too many contours, clipping at 1000', contours);
break;
}
}
return pathinfo;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ContourCarpet = {};
ContourCarpet.attributes = require('./attributes');
ContourCarpet.supplyDefaults = require('./defaults');
ContourCarpet.colorbar = require('../contour/colorbar');
ContourCarpet.calc = require('./calc');
ContourCarpet.plot = require('./plot');
ContourCarpet.style = require('./style');
ContourCarpet.moduleType = 'trace';
ContourCarpet.name = 'contourcarpet';
ContourCarpet.basePlotModule = require('../../plots/cartesian');
ContourCarpet.categories = ['cartesian', 'carpet', 'contour', 'symbols', 'showLegend', 'hasLines', 'carpetDependent'];
ContourCarpet.meta = {
hrName: 'contour_carpet',
description: [
'Plots contours on either the first carpet axis or the',
'carpet axis with a matching `carpet` attribute. Data `z`',
'is interpreted as matching that of the corresponding carpet',
'axis.'
].join(' ')
};
module.exports = ContourCarpet;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Drawing = require('../../components/drawing');
var axisAlignedLine = require('../carpet/axis_aligned_line');
var Lib = require('../../lib');
// var map1dArray = require('../carpet/map_1d_array');
// var makepath = require('../carpet/makepath');
module.exports = function joinAllPaths(trace, pi, perimeter, ab2p, carpet, carpetcd, xa, ya) {
var i;
var fullpath = '';
var startsleft = pi.edgepaths.map(function(v, i) { return i; });
var newloop = true;
var endpt, newendpt, cnt, nexti, possiblei, addpath;
var atol = Math.abs(perimeter[0][0] - perimeter[2][0]) * 1e-4;
var btol = Math.abs(perimeter[0][1] - perimeter[2][1]) * 1e-4;
function istop(pt) { return Math.abs(pt[1] - perimeter[0][1]) < btol; }
function isbottom(pt) { return Math.abs(pt[1] - perimeter[2][1]) < btol; }
function isleft(pt) { return Math.abs(pt[0] - perimeter[0][0]) < atol; }
function isright(pt) { return Math.abs(pt[0] - perimeter[2][0]) < atol; }
function pathto(pt0, pt1) {
var i, j, segments, axis;
var path = '';
if((istop(pt0) && !isright(pt0)) || (isbottom(pt0) && !isleft(pt0))) {
axis = carpet.aaxis;
segments = axisAlignedLine(carpet, carpetcd, [pt0[0], pt1[0]], 0.5 * (pt0[1] + pt1[1]));
} else {
axis = carpet.baxis;
segments = axisAlignedLine(carpet, carpetcd, 0.5 * (pt0[0] + pt1[0]), [pt0[1], pt1[1]]);
}
for(i = 1; i < segments.length; i++) {
path += axis.smoothing ? 'C' : 'L';
for(j = 0; j < segments[i].length; j++) {
var pt = segments[i][j];
path += [xa.c2p(pt[0]), ya.c2p(pt[1])] + ' ';
}
}
return path;
}
i = 0;
endpt = null;
while(startsleft.length) {
var startpt = pi.edgepaths[i][0];
if(endpt) {
fullpath += pathto(endpt, startpt);
}
addpath = Drawing.smoothopen(pi.edgepaths[i].map(ab2p), pi.smoothing);
fullpath += newloop ? addpath : addpath.replace(/^M/, 'L');
startsleft.splice(startsleft.indexOf(i), 1);
endpt = pi.edgepaths[i][pi.edgepaths[i].length - 1];
nexti = -1;
// now loop through sides, moving our endpoint until we find a new start
for(cnt = 0; cnt < 4; cnt++) { // just to prevent infinite loops
if(!endpt) {
Lib.log('Missing end?', i, pi);
break;
}
if(istop(endpt) && !isright(endpt)) {
newendpt = perimeter[1]; // left top ---> right top
} else if(isleft(endpt)) {
newendpt = perimeter[0]; // left bottom ---> left top
} else if(isbottom(endpt)) {
newendpt = perimeter[3]; // right bottom
} else if(isright(endpt)) {
newendpt = perimeter[2]; // left bottom
}
for(possiblei = 0; possiblei < pi.edgepaths.length; possiblei++) {
var ptNew = pi.edgepaths[possiblei][0];
// is ptNew on the (horz. or vert.) segment from endpt to newendpt?
if(Math.abs(endpt[0] - newendpt[0]) < atol) {
if(Math.abs(endpt[0] - ptNew[0]) < atol && (ptNew[1] - endpt[1]) * (newendpt[1] - ptNew[1]) >= 0) {
newendpt = ptNew;
nexti = possiblei;
}
} else if(Math.abs(endpt[1] - newendpt[1]) < btol) {
if(Math.abs(endpt[1] - ptNew[1]) < btol && (ptNew[0] - endpt[0]) * (newendpt[0] - ptNew[0]) >= 0) {
newendpt = ptNew;
nexti = possiblei;
}
} else {
Lib.log('endpt to newendpt is not vert. or horz.', endpt, newendpt, ptNew);
}
}
if(nexti >= 0) break;
fullpath += pathto(endpt, newendpt);
endpt = newendpt;
}
if(nexti === pi.edgepaths.length) {
Lib.log('unclosed perimeter path');
break;
}
i = nexti;
// if we closed back on a loop we already included,
// close it and start a new loop
newloop = (startsleft.indexOf(i) === -1);
if(newloop) {
i = startsleft[0];
fullpath += pathto(endpt, newendpt) + 'Z';
endpt = null;
}
}
// finally add the interior paths
for(i = 0; i < pi.paths.length; i++) {
fullpath += Drawing.smoothclosed(pi.paths[i].map(ab2p), pi.smoothing);
}
return fullpath;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function mapPathinfo(pathinfo, map) { var i, j, k, pi, pedgepaths, ppaths, pedgepath, ppath, path; for(i = 0; i < pathinfo.length; i++) { pi = pathinfo[i]; pedgepaths = pi.pedgepaths = []; ppaths = pi.ppaths = []; for(j = 0; j < pi.edgepaths.length; j++) { path = pi.edgepaths[j]; pedgepath = []; for(k = 0; k < path.length; k++) { pedgepath[k] = map(path[k]); } pedgepaths.push(pedgepath); } for(j = 0; j < pi.paths.length; j++) { path = pi.paths[j]; ppath = []; for(k = 0; k < path.length; k++) { ppath[k] = map(path[k]); } ppaths.push(ppath); } } }; |
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| index.js | 16.67% | (2 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (2 / 12) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ContourGl = {};
ContourGl.attributes = require('../contour/attributes');
ContourGl.supplyDefaults = require('../contour/defaults');
ContourGl.colorbar = require('../contour/colorbar');
ContourGl.calc = require('../contour/calc');
ContourGl.plot = require('./convert');
ContourGl.moduleType = 'trace';
ContourGl.name = 'contourgl';
ContourGl.basePlotModule = require('../../plots/gl2d');
ContourGl.categories = ['gl2d', '2dMap'];
ContourGl.meta = {
description: [
'WebGL contour (beta)'
].join(' ')
};
module.exports = ContourGl;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 20% | (1 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 20% | (1 / 5) | |
| clean_2d_array.js | 10.34% | (3 / 29) | 0% | (0 / 6) | 0% | (0 / 6) | 14.29% | (3 / 21) | |
| colorbar.js | 33.33% | (6 / 18) | 0% | (0 / 6) | 0% | (0 / 1) | 37.5% | (6 / 16) | |
| convert_column_xyz.js | 7.14% | (3 / 42) | 0% | (0 / 24) | 0% | (0 / 1) | 8.33% | (3 / 36) | |
| find_empties.js | 4.76% | (2 / 42) | 0% | (0 / 36) | 0% | (0 / 2) | 5.41% | (2 / 37) | |
| has_columns.js | 50% | (1 / 2) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (1 / 2) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) | |
| interp2d.js | 9.62% | (5 / 52) | 0% | (0 / 22) | 0% | (0 / 3) | 10.2% | (5 / 49) | |
| make_bound_array.js | 6.06% | (2 / 33) | 0% | (0 / 33) | 0% | (0 / 1) | 6.9% | (2 / 29) | |
| max_row_length.js | 20% | (1 / 5) | 100% | (0 / 0) | 0% | (0 / 1) | 20% | (1 / 5) | |
| xyz_defaults.js | 13.95% | (6 / 43) | 0% | (0 / 29) | 0% | (0 / 3) | 15.38% | (6 / 39) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 12 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = extendFlat({}, {
z: {
valType: 'data_array',
description: 'Sets the z data.'
},
x: scatterAttrs.x,
x0: scatterAttrs.x0,
dx: scatterAttrs.dx,
y: scatterAttrs.y,
y0: scatterAttrs.y0,
dy: scatterAttrs.dy,
text: {
valType: 'data_array',
description: 'Sets the text elements associated with each z value.'
},
transpose: {
valType: 'boolean',
dflt: false,
role: 'info',
description: 'Transposes the z data.'
},
xtype: {
valType: 'enumerated',
values: ['array', 'scaled'],
role: 'info',
description: [
'If *array*, the heatmap\'s x coordinates are given by *x*',
'(the default behavior when `x` is provided).',
'If *scaled*, the heatmap\'s x coordinates are given by *x0* and *dx*',
'(the default behavior when `x` is not provided).'
].join(' ')
},
ytype: {
valType: 'enumerated',
values: ['array', 'scaled'],
role: 'info',
description: [
'If *array*, the heatmap\'s y coordinates are given by *y*',
'(the default behavior when `y` is provided)',
'If *scaled*, the heatmap\'s y coordinates are given by *y0* and *dy*',
'(the default behavior when `y` is not provided)'
].join(' ')
},
zsmooth: {
valType: 'enumerated',
values: ['fast', 'best', false],
dflt: false,
role: 'style',
description: [
'Picks a smoothing algorithm use to smooth `z` data.'
].join(' ')
},
connectgaps: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'Determines whether or not gaps',
'(i.e. {nan} or missing values)',
'in the `z` data are filled in.'
].join(' ')
},
xgap: {
valType: 'number',
dflt: 0,
min: 0,
role: 'style',
description: 'Sets the horizontal gap (in pixels) between bricks.'
},
ygap: {
valType: 'number',
dflt: 0,
min: 0,
role: 'style',
description: 'Sets the vertical gap (in pixels) between bricks.'
},
},
colorscaleAttrs,
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
module.exports = function clean2dArray(zOld, transpose) {
var rowlen, collen, getCollen, old2new, i, j;
function cleanZvalue(v) {
if(!isNumeric(v)) return undefined;
return +v;
}
if(transpose) {
rowlen = 0;
for(i = 0; i < zOld.length; i++) rowlen = Math.max(rowlen, zOld[i].length);
if(rowlen === 0) return false;
getCollen = function(zOld) { return zOld.length; };
old2new = function(zOld, i, j) { return zOld[j][i]; };
}
else {
rowlen = zOld.length;
getCollen = function(zOld, i) { return zOld[i].length; };
old2new = function(zOld, i, j) { return zOld[i][j]; };
}
var zNew = new Array(rowlen);
for(i = 0; i < rowlen; i++) {
collen = getCollen(zOld, i);
zNew[i] = new Array(collen);
for(j = 0; j < collen; j++) zNew[i][j] = cleanZvalue(old2new(zOld, i, j));
}
return zNew;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var Plots = require('../../plots/plots');
var Colorscale = require('../../components/colorscale');
var drawColorbar = require('../../components/colorbar/draw');
module.exports = function colorbar(gd, cd) {
var trace = cd[0].trace,
cbId = 'cb' + trace.uid,
zmin = trace.zmin,
zmax = trace.zmax;
if(!isNumeric(zmin)) zmin = Lib.aggNums(Math.min, null, trace.z);
if(!isNumeric(zmax)) zmax = Lib.aggNums(Math.max, null, trace.z);
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
if(!trace.showscale) {
Plots.autoMargin(gd, cbId);
return;
}
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
var sclFunc = Colorscale.makeColorScaleFunc(
Colorscale.extractScale(
trace.colorscale,
zmin,
zmax
),
{ noNumericCheck: true }
);
cb.fillcolor(sclFunc)
.filllevels({start: zmin, end: zmax, size: (zmax - zmin) / 254})
.options(trace.colorbar)();
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var BADNUM = require('../../constants/numerical').BADNUM;
module.exports = function convertColumnData(trace, ax1, ax2, var1Name, var2Name, arrayVarNames) {
var1Name = var1Name || 'x';
var2Name = var2Name || 'y';
arrayVarNames = arrayVarNames || ['z'];
var col1 = trace[var1Name].slice(),
col2 = trace[var2Name].slice(),
textCol = trace.text,
colLen = Math.min(col1.length, col2.length),
hasColumnText = (textCol !== undefined && !Array.isArray(textCol[0])),
col1Calendar = trace[var1Name + 'calendar'],
col2Calendar = trace[var2Name + 'calendar'];
var i, j, arrayVar, newArray, arrayVarName;
for(i = 0; i < arrayVarNames.length; i++) {
arrayVar = trace[arrayVarNames[i]];
if(arrayVar) colLen = Math.min(colLen, arrayVar.length);
}
if(colLen < col1.length) col1 = col1.slice(0, colLen);
if(colLen < col2.length) col2 = col2.slice(0, colLen);
for(i = 0; i < colLen; i++) {
col1[i] = ax1.d2c(col1[i], 0, col1Calendar);
col2[i] = ax2.d2c(col2[i], 0, col2Calendar);
}
var col1dv = Lib.distinctVals(col1),
col1vals = col1dv.vals,
col2dv = Lib.distinctVals(col2),
col2vals = col2dv.vals,
newArrays = [];
for(i = 0; i < arrayVarNames.length; i++) {
newArrays[i] = Lib.init2dArray(col2vals.length, col1vals.length);
}
var i1, i2, text;
if(hasColumnText) text = Lib.init2dArray(col2vals.length, col1vals.length);
for(i = 0; i < colLen; i++) {
if(col1[i] !== BADNUM && col2[i] !== BADNUM) {
i1 = Lib.findBin(col1[i] + col1dv.minDiff / 2, col1vals);
i2 = Lib.findBin(col2[i] + col2dv.minDiff / 2, col2vals);
for(j = 0; j < arrayVarNames.length; j++) {
arrayVarName = arrayVarNames[j];
arrayVar = trace[arrayVarName];
newArray = newArrays[j];
newArray[i2][i1] = arrayVar[i];
}
if(hasColumnText) text[i2][i1] = textCol[i];
}
}
trace[var1Name] = col1vals;
trace[var2Name] = col2vals;
for(j = 0; j < arrayVarNames.length; j++) {
trace[arrayVarNames[j]] = newArrays[j];
}
if(hasColumnText) trace.text = text;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var maxRowLength = require('./max_row_length');
/* Return a list of empty points in 2D array z
* each empty point z[i][j] gives an array [i, j, neighborCount]
* neighborCount is the count of 4 nearest neighbors that DO exist
* this is to give us an order of points to evaluate for interpolation.
* if no neighbors exist, we iteratively look for neighbors that HAVE
* neighbors, and add a fractional neighborCount
*/
module.exports = function findEmpties(z) {
var empties = [],
neighborHash = {},
noNeighborList = [],
nextRow = z[0],
row = [],
blank = [0, 0, 0],
rowLength = maxRowLength(z),
prevRow,
i,
j,
thisPt,
p,
neighborCount,
newNeighborHash,
foundNewNeighbors;
for(i = 0; i < z.length; i++) {
prevRow = row;
row = nextRow;
nextRow = z[i + 1] || [];
for(j = 0; j < rowLength; j++) {
if(row[j] === undefined) {
neighborCount = (row[j - 1] !== undefined ? 1 : 0) +
(row[j + 1] !== undefined ? 1 : 0) +
(prevRow[j] !== undefined ? 1 : 0) +
(nextRow[j] !== undefined ? 1 : 0);
if(neighborCount) {
// for this purpose, don't count off-the-edge points
// as undefined neighbors
if(i === 0) neighborCount++;
if(j === 0) neighborCount++;
if(i === z.length - 1) neighborCount++;
if(j === row.length - 1) neighborCount++;
// if all neighbors that could exist do, we don't
// need this for finding farther neighbors
if(neighborCount < 4) {
neighborHash[[i, j]] = [i, j, neighborCount];
}
empties.push([i, j, neighborCount]);
}
else noNeighborList.push([i, j]);
}
}
}
while(noNeighborList.length) {
newNeighborHash = {};
foundNewNeighbors = false;
// look for cells that now have neighbors but didn't before
for(p = noNeighborList.length - 1; p >= 0; p--) {
thisPt = noNeighborList[p];
i = thisPt[0];
j = thisPt[1];
neighborCount = ((neighborHash[[i - 1, j]] || blank)[2] +
(neighborHash[[i + 1, j]] || blank)[2] +
(neighborHash[[i, j - 1]] || blank)[2] +
(neighborHash[[i, j + 1]] || blank)[2]) / 20;
if(neighborCount) {
newNeighborHash[thisPt] = [i, j, neighborCount];
noNeighborList.splice(p, 1);
foundNewNeighbors = true;
}
}
if(!foundNewNeighbors) {
throw 'findEmpties iterated with no new neighbors';
}
// put these new cells into the main neighbor list
for(thisPt in newNeighborHash) {
neighborHash[thisPt] = newNeighborHash[thisPt];
empties.push(newNeighborHash[thisPt]);
}
}
// sort the full list in descending order of neighbor count
return empties.sort(function(a, b) { return b[2] - a[2]; });
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function(trace) { return !Array.isArray(trace.z[0]); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Heatmap = {};
Heatmap.attributes = require('./attributes');
Heatmap.supplyDefaults = require('./defaults');
Heatmap.calc = require('./calc');
Heatmap.plot = require('./plot');
Heatmap.colorbar = require('./colorbar');
Heatmap.style = require('./style');
Heatmap.hoverPoints = require('./hover');
Heatmap.moduleType = 'trace';
Heatmap.name = 'heatmap';
Heatmap.basePlotModule = require('../../plots/cartesian');
Heatmap.categories = ['cartesian', '2dMap'];
Heatmap.meta = {
description: [
'The data that describes the heatmap value-to-color mapping',
'is set in `z`.',
'Data in `z` can either be a {2D array} of values (ragged or not)',
'or a 1D array of values.',
'In the case where `z` is a {2D array},',
'say that `z` has N rows and M columns.',
'Then, by default, the resulting heatmap will have N partitions along',
'the y axis and M partitions along the x axis.',
'In other words, the i-th row/ j-th column cell in `z`',
'is mapped to the i-th partition of the y axis',
'(starting from the bottom of the plot) and the j-th partition',
'of the x-axis (starting from the left of the plot).',
'This behavior can be flipped by using `transpose`.',
'Moreover, `x` (`y`) can be provided with M or M+1 (N or N+1) elements.',
'If M (N), then the coordinates correspond to the center of the',
'heatmap cells and the cells have equal width.',
'If M+1 (N+1), then the coordinates correspond to the edges of the',
'heatmap cells.',
'In the case where `z` is a 1D {array}, the x and y coordinates must be',
'provided in `x` and `y` respectively to form data triplets.'
].join(' ')
};
module.exports = Heatmap;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var INTERPTHRESHOLD = 1e-2,
NEIGHBORSHIFTS = [[-1, 0], [1, 0], [0, -1], [0, 1]];
function correctionOvershoot(maxFractionalChange) {
// start with less overshoot, until we know it's converging,
// then ramp up the overshoot for faster convergence
return 0.5 - 0.25 * Math.min(1, maxFractionalChange * 0.5);
}
module.exports = function interp2d(z, emptyPoints, savedInterpZ) {
// fill in any missing data in 2D array z using an iterative
// poisson equation solver with zero-derivative BC at edges
// amazingly, this just amounts to repeatedly averaging all the existing
// nearest neighbors (at least if we don't take x/y scaling into account)
var maxFractionalChange = 1,
i,
thisPt;
if(Array.isArray(savedInterpZ)) {
for(i = 0; i < emptyPoints.length; i++) {
thisPt = emptyPoints[i];
z[thisPt[0]][thisPt[1]] = savedInterpZ[thisPt[0]][thisPt[1]];
}
}
else {
// one pass to fill in a starting value for all the empties
iterateInterp2d(z, emptyPoints);
}
// we're don't need to iterate lone empties - remove them
for(i = 0; i < emptyPoints.length; i++) {
if(emptyPoints[i][2] < 4) break;
}
// but don't remove these points from the original array,
// we'll use them for masking, so make a copy.
emptyPoints = emptyPoints.slice(i);
for(i = 0; i < 100 && maxFractionalChange > INTERPTHRESHOLD; i++) {
maxFractionalChange = iterateInterp2d(z, emptyPoints,
correctionOvershoot(maxFractionalChange));
}
if(maxFractionalChange > INTERPTHRESHOLD) {
Lib.log('interp2d didn\'t converge quickly', maxFractionalChange);
}
return z;
};
function iterateInterp2d(z, emptyPoints, overshoot) {
var maxFractionalChange = 0,
thisPt,
i,
j,
p,
q,
neighborShift,
neighborRow,
neighborVal,
neighborCount,
neighborSum,
initialVal,
minNeighbor,
maxNeighbor;
for(p = 0; p < emptyPoints.length; p++) {
thisPt = emptyPoints[p];
i = thisPt[0];
j = thisPt[1];
initialVal = z[i][j];
neighborSum = 0;
neighborCount = 0;
for(q = 0; q < 4; q++) {
neighborShift = NEIGHBORSHIFTS[q];
neighborRow = z[i + neighborShift[0]];
if(!neighborRow) continue;
neighborVal = neighborRow[j + neighborShift[1]];
if(neighborVal !== undefined) {
if(neighborSum === 0) {
minNeighbor = maxNeighbor = neighborVal;
}
else {
minNeighbor = Math.min(minNeighbor, neighborVal);
maxNeighbor = Math.max(maxNeighbor, neighborVal);
}
neighborCount++;
neighborSum += neighborVal;
}
}
if(neighborCount === 0) {
throw 'iterateInterp2d order is wrong: no defined neighbors';
}
// this is the laplace equation interpolation:
// each point is just the average of its neighbors
// note that this ignores differential x/y scaling
// which I think is the right approach, since we
// don't know what that scaling means
z[i][j] = neighborSum / neighborCount;
if(initialVal === undefined) {
if(neighborCount < 4) maxFractionalChange = 1;
}
else {
// we can make large empty regions converge faster
// if we overshoot the change vs the previous value
z[i][j] = (1 + overshoot) * z[i][j] - overshoot * initialVal;
if(maxNeighbor > minNeighbor) {
maxFractionalChange = Math.max(maxFractionalChange,
Math.abs(z[i][j] - initialVal) / (maxNeighbor - minNeighbor));
}
}
}
return maxFractionalChange;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
module.exports = function makeBoundArray(trace, arrayIn, v0In, dvIn, numbricks, ax) {
var arrayOut = [],
isContour = Registry.traceIs(trace, 'contour'),
isHist = Registry.traceIs(trace, 'histogram'),
isGL2D = Registry.traceIs(trace, 'gl2d'),
v0,
dv,
i;
var isArrayOfTwoItemsOrMore = Array.isArray(arrayIn) && arrayIn.length > 1;
if(isArrayOfTwoItemsOrMore && !isHist && (ax.type !== 'category')) {
var len = arrayIn.length;
// given vals are brick centers
// hopefully length === numbricks, but use this method even if too few are supplied
// and extend it linearly based on the last two points
if(len <= numbricks) {
// contour plots only want the centers
if(isContour || isGL2D) arrayOut = arrayIn.slice(0, numbricks);
else if(numbricks === 1) {
arrayOut = [arrayIn[0] - 0.5, arrayIn[0] + 0.5];
}
else {
arrayOut = [1.5 * arrayIn[0] - 0.5 * arrayIn[1]];
for(i = 1; i < len; i++) {
arrayOut.push((arrayIn[i - 1] + arrayIn[i]) * 0.5);
}
arrayOut.push(1.5 * arrayIn[len - 1] - 0.5 * arrayIn[len - 2]);
}
if(len < numbricks) {
var lastPt = arrayOut[arrayOut.length - 1],
delta = lastPt - arrayOut[arrayOut.length - 2];
for(i = len; i < numbricks; i++) {
lastPt += delta;
arrayOut.push(lastPt);
}
}
}
else {
// hopefully length === numbricks+1, but do something regardless:
// given vals are brick boundaries
return isContour ?
arrayIn.slice(0, numbricks) : // we must be strict for contours
arrayIn.slice(0, numbricks + 1);
}
}
else {
dv = dvIn || 1;
var calendar = trace[ax._id.charAt(0) + 'calendar'];
if(isHist || ax.type === 'category') v0 = ax.r2c(v0In, 0, calendar) || 0;
else if(Array.isArray(arrayIn) && arrayIn.length === 1) v0 = arrayIn[0];
else if(v0In === undefined) v0 = 0;
else v0 = ax.d2c(v0In, 0, calendar);
for(i = (isContour || isGL2D) ? 0 : -0.5; i < numbricks; i++) {
arrayOut.push(v0 + dv * i);
}
}
return arrayOut;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function maxRowLength(z) { var len = 0; for(var i = 0; i < z.length; i++) { len = Math.max(len, z[i].length); } return len; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var hasColumns = require('./has_columns');
module.exports = function handleXYZDefaults(traceIn, traceOut, coerce, layout, xName, yName) {
var z = coerce('z');
xName = xName || 'x';
yName = yName || 'y';
var x, y;
if(z === undefined || !z.length) return 0;
if(hasColumns(traceIn)) {
x = coerce(xName);
y = coerce(yName);
// column z must be accompanied by xName and yName arrays
if(!x || !y) return 0;
}
else {
x = coordDefaults(xName, coerce);
y = coordDefaults(yName, coerce);
// TODO put z validation elsewhere
if(!isValidZ(z)) return 0;
coerce('transpose');
}
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, [xName, yName], layout);
return traceOut.z.length;
};
function coordDefaults(coordStr, coerce) {
var coord = coerce(coordStr),
coordType = coord ?
coerce(coordStr + 'type', 'array') :
'scaled';
if(coordType === 'scaled') {
coerce(coordStr + '0');
coerce('d' + coordStr);
}
return coord;
}
function isValidZ(z) {
var allRowsAreArrays = true,
oneRowIsFilled = false,
hasOneNumber = false,
zi;
/*
* Without this step:
*
* hasOneNumber = false breaks contour but not heatmap
* allRowsAreArrays = false breaks contour but not heatmap
* oneRowIsFilled = false breaks both
*/
for(var i = 0; i < z.length; i++) {
zi = z[i];
if(!Array.isArray(zi)) {
allRowsAreArrays = false;
break;
}
if(zi.length > 0) oneRowIsFilled = true;
for(var j = 0; j < zi.length; j++) {
if(isNumeric(zi[j])) {
hasOneNumber = true;
break;
}
}
}
return (allRowsAreArrays && oneRowIsFilled && hasOneNumber);
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 9.09% | (1 / 11) | 100% | (0 / 0) | 100% | (0 / 0) | 9.09% | (1 / 11) | |
| index.js | 16.67% | (2 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (2 / 12) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var heatmapAttrs = require('../heatmap/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var commonList = [
'z',
'x', 'x0', 'dx',
'y', 'y0', 'dy',
'text', 'transpose',
'xtype', 'ytype'
];
var attrs = {};
for(var i = 0; i < commonList.length; i++) {
var k = commonList[i];
attrs[k] = heatmapAttrs[k];
}
extendFlat(
attrs,
colorscaleAttrs,
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },
{ colorbar: colorbarAttrs }
);
module.exports = attrs;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var HeatmapGl = {};
HeatmapGl.attributes = require('./attributes');
HeatmapGl.supplyDefaults = require('../heatmap/defaults');
HeatmapGl.colorbar = require('../heatmap/colorbar');
HeatmapGl.calc = require('../heatmap/calc');
HeatmapGl.plot = require('./convert');
HeatmapGl.moduleType = 'trace';
HeatmapGl.name = 'heatmapgl';
HeatmapGl.basePlotModule = require('../../plots/gl2d');
HeatmapGl.categories = ['gl2d', '2dMap'];
HeatmapGl.meta = {
description: [
'WebGL version of the heatmap trace type.'
].join(' ')
};
module.exports = HeatmapGl;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 50% | (2 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (2 / 4) | |
| average.js | 12.5% | (1 / 8) | 0% | (0 / 2) | 0% | (0 / 1) | 12.5% | (1 / 8) | |
| bin_defaults.js | 11.11% | (1 / 9) | 100% | (0 / 0) | 0% | (0 / 2) | 11.11% | (1 / 9) | |
| bin_functions.js | 5.26% | (2 / 38) | 0% | (0 / 16) | 0% | (0 / 5) | 5.26% | (2 / 38) | |
| clean_bins.js | 21.43% | (6 / 28) | 0% | (0 / 35) | 0% | (0 / 3) | 23.08% | (6 / 26) | |
| index.js | 11.76% | (2 / 17) | 100% | (0 / 0) | 100% | (0 / 0) | 11.76% | (2 / 17) | |
| norm_functions.js | 6.25% | (1 / 16) | 0% | (0 / 4) | 0% | (0 / 4) | 9.09% | (1 / 11) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | 6 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var barAttrs = require('../bar/attributes');
module.exports = {
x: {
valType: 'data_array',
description: [
'Sets the sample data to be binned on the x axis.'
].join(' ')
},
y: {
valType: 'data_array',
description: [
'Sets the sample data to be binned on the y axis.'
].join(' ')
},
text: barAttrs.text,
orientation: barAttrs.orientation,
histfunc: {
valType: 'enumerated',
values: ['count', 'sum', 'avg', 'min', 'max'],
role: 'style',
dflt: 'count',
description: [
'Specifies the binning function used for this histogram trace.',
'If *count*, the histogram values are computed by counting the',
'number of values lying inside each bin.',
'If *sum*, *avg*, *min*, *max*,',
'the histogram values are computed using',
'the sum, the average, the minimum or the maximum',
'of the values lying inside each bin respectively.'
].join(' ')
},
histnorm: {
valType: 'enumerated',
values: ['', 'percent', 'probability', 'density', 'probability density'],
dflt: '',
role: 'style',
description: [
'Specifies the type of normalization used for this histogram trace.',
'If **, the span of each bar corresponds to the number of',
'occurrences (i.e. the number of data points lying inside the bins).',
'If *percent* / *probability*, the span of each bar corresponds to',
'the percentage / fraction of occurrences with respect to the total',
'number of sample points',
'(here, the sum of all bin HEIGHTS equals 100% / 1).',
'If *density*, the span of each bar corresponds to the number of',
'occurrences in a bin divided by the size of the bin interval',
'(here, the sum of all bin AREAS equals the',
'total number of sample points).',
'If *probability density*, the area of each bar corresponds to the',
'probability that an event will fall into the corresponding bin',
'(here, the sum of all bin AREAS equals 1).'
].join(' ')
},
cumulative: {
enabled: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'If true, display the cumulative distribution by summing the',
'binned values. Use the `direction` and `centralbin` attributes',
'to tune the accumulation method.',
'Note: in this mode, the *density* `histnorm` settings behave',
'the same as their equivalents without *density*:',
'** and *density* both rise to the number of data points, and',
'*probability* and *probability density* both rise to the',
'number of sample points.'
].join(' ')
},
direction: {
valType: 'enumerated',
values: ['increasing', 'decreasing'],
dflt: 'increasing',
role: 'info',
description: [
'Only applies if cumulative is enabled.',
'If *increasing* (default) we sum all prior bins, so the result',
'increases from left to right. If *decreasing* we sum later bins',
'so the result decreases from left to right.'
].join(' ')
},
currentbin: {
valType: 'enumerated',
values: ['include', 'exclude', 'half'],
dflt: 'include',
role: 'info',
description: [
'Only applies if cumulative is enabled.',
'Sets whether the current bin is included, excluded, or has half',
'of its value included in the current cumulative value.',
'*include* is the default for compatibility with various other',
'tools, however it introduces a half-bin bias to the results.',
'*exclude* makes the opposite half-bin bias, and *half* removes',
'it.'
].join(' ')
}
},
autobinx: {
valType: 'boolean',
dflt: null,
role: 'style',
description: [
'Determines whether or not the x axis bin attributes are picked',
'by an algorithm. Note that this should be set to false if you',
'want to manually set the number of bins using the attributes in',
'xbins.'
].join(' ')
},
nbinsx: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'style',
description: [
'Specifies the maximum number of desired bins. This value will be used',
'in an algorithm that will decide the optimal bin size such that the',
'histogram best visualizes the distribution of the data.'
].join(' ')
},
xbins: makeBinsAttr('x'),
autobiny: {
valType: 'boolean',
dflt: null,
role: 'style',
description: [
'Determines whether or not the y axis bin attributes are picked',
'by an algorithm. Note that this should be set to false if you',
'want to manually set the number of bins using the attributes in',
'ybins.'
].join(' ')
},
nbinsy: {
valType: 'integer',
min: 0,
dflt: 0,
role: 'style',
description: [
'Specifies the maximum number of desired bins. This value will be used',
'in an algorithm that will decide the optimal bin size such that the',
'histogram best visualizes the distribution of the data.'
].join(' ')
},
ybins: makeBinsAttr('y'),
marker: barAttrs.marker,
error_y: barAttrs.error_y,
error_x: barAttrs.error_x,
_deprecated: {
bardir: barAttrs._deprecated.bardir
}
};
function makeBinsAttr(axLetter) {
return {
start: {
valType: 'any', // for date axes
dflt: null,
role: 'style',
description: [
'Sets the starting value for the', axLetter,
'axis bins.'
].join(' ')
},
end: {
valType: 'any', // for date axes
dflt: null,
role: 'style',
description: [
'Sets the end value for the', axLetter,
'axis bins.'
].join(' ')
},
size: {
valType: 'any', // for date axes
dflt: null,
role: 'style',
description: [
'Sets the step in-between value each', axLetter,
'axis bin.'
].join(' ')
}
};
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function doAvg(size, counts) { var nMax = size.length, total = 0; for(var i = 0; i < nMax; i++) { if(counts[i]) { size[i] /= counts[i]; total += size[i]; } else size[i] = null; } return total; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function handleBinDefaults(traceIn, traceOut, coerce, binDirections) { coerce('histnorm'); binDirections.forEach(function(binDirection) { /* * Because date axes have string values for start and end, * and string options for size, we cannot validate these attributes * now. We will do this during calc (immediately prior to binning) * in ./clean_bins, and push the cleaned values back to _fullData. */ coerce(binDirection + 'bins.start'); coerce(binDirection + 'bins.end'); coerce(binDirection + 'bins.size'); coerce('autobin' + binDirection); coerce('nbins' + binDirection); }); return traceOut; }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
module.exports = {
count: function(n, i, size) {
size[n]++;
return 1;
},
sum: function(n, i, size, counterData) {
var v = counterData[i];
if(isNumeric(v)) {
v = Number(v);
size[n] += v;
return v;
}
return 0;
},
avg: function(n, i, size, counterData, counts) {
var v = counterData[i];
if(isNumeric(v)) {
v = Number(v);
size[n] += v;
counts[n]++;
}
return 0;
},
min: function(n, i, size, counterData) {
var v = counterData[i];
if(isNumeric(v)) {
v = Number(v);
if(!isNumeric(size[n])) {
size[n] = v;
return v;
}
else if(size[n] > v) {
var delta = v - size[n];
size[n] = v;
return delta;
}
}
return 0;
},
max: function(n, i, size, counterData) {
var v = counterData[i];
if(isNumeric(v)) {
v = Number(v);
if(!isNumeric(size[n])) {
size[n] = v;
return v;
}
else if(size[n] < v) {
var delta = v - size[n];
size[n] = v;
return delta;
}
}
return 0;
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var cleanDate = require('../../lib').cleanDate;
var constants = require('../../constants/numerical');
var ONEDAY = constants.ONEDAY;
var BADNUM = constants.BADNUM;
/*
* cleanBins: validate attributes autobin[xy] and [xy]bins.(start, end, size)
* Mutates trace so all these attributes are valid.
*
* Normally this kind of thing would happen during supplyDefaults, but
* in this case we need to know the axis type, and axis type isn't set until
* after trace supplyDefaults are completed. So this gets called during the
* calc step, when data are inserted into bins.
*/
module.exports = function cleanBins(trace, ax, binDirection) {
var axType = ax.type,
binAttr = binDirection + 'bins',
bins = trace[binAttr];
if(!bins) bins = trace[binAttr] = {};
var cleanBound = (axType === 'date') ?
function(v) { return (v || v === 0) ? cleanDate(v, BADNUM, bins.calendar) : null; } :
function(v) { return isNumeric(v) ? Number(v) : null; };
bins.start = cleanBound(bins.start);
bins.end = cleanBound(bins.end);
// logic for bin size is very similar to dtick (cartesian/tick_value_defaults)
// but without the extra string options for log axes
// ie the only strings we accept are M<n> for months
var sizeDflt = (axType === 'date') ? ONEDAY : 1,
binSize = bins.size;
if(isNumeric(binSize)) {
bins.size = (binSize > 0) ? Number(binSize) : sizeDflt;
}
else if(typeof binSize !== 'string') {
bins.size = sizeDflt;
}
else {
// date special case: "M<n>" gives bins every (integer) n months
var prefix = binSize.charAt(0),
sizeNum = binSize.substr(1);
sizeNum = isNumeric(sizeNum) ? Number(sizeNum) : 0;
if((sizeNum <= 0) || !(
axType === 'date' && prefix === 'M' && sizeNum === Math.round(sizeNum)
)) {
bins.size = sizeDflt;
}
}
var autoBinAttr = 'autobin' + binDirection;
if(typeof trace[autoBinAttr] !== 'boolean') {
trace[autoBinAttr] = !(
(bins.start || bins.start === 0) &&
(bins.end || bins.end === 0)
);
}
if(!trace[autoBinAttr]) delete trace['nbins' + binDirection];
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
/**
* Histogram has its own attribute, defaults and calc steps,
* but uses bar's plot to display
* and bar's setPositions for stacking and grouping
*/
/**
* histogram errorBarsOK is debatable, but it's put in for backward compat.
* there are use cases for it - sqrt for a simple histogram works right now,
* constant and % work but they're not so meaningful. I guess it could be cool
* to allow quadrature combination of errors in summed histograms...
*/
var Histogram = {};
Histogram.attributes = require('./attributes');
Histogram.layoutAttributes = require('../bar/layout_attributes');
Histogram.supplyDefaults = require('./defaults');
Histogram.supplyLayoutDefaults = require('../bar/layout_defaults');
Histogram.calc = require('./calc');
Histogram.setPositions = require('../bar/set_positions');
Histogram.plot = require('../bar/plot');
Histogram.style = require('../bar/style');
Histogram.colorbar = require('../scatter/colorbar');
Histogram.hoverPoints = require('../bar/hover');
Histogram.moduleType = 'trace';
Histogram.name = 'histogram';
Histogram.basePlotModule = require('../../plots/cartesian');
Histogram.categories = ['cartesian', 'bar', 'histogram', 'oriented', 'errorBarsOK', 'showLegend'];
Histogram.meta = {
description: [
'The sample data from which statistics are computed is set in `x`',
'for vertically spanning histograms and',
'in `y` for horizontally spanning histograms.',
'Binning options are set `xbins` and `ybins` respectively',
'if no aggregation data is provided.'
].join(' ')
};
module.exports = Histogram;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
percent: function(size, total) {
var nMax = size.length,
norm = 100 / total;
for(var n = 0; n < nMax; n++) size[n] *= norm;
},
probability: function(size, total) {
var nMax = size.length;
for(var n = 0; n < nMax; n++) size[n] /= total;
},
density: function(size, total, inc, yinc) {
var nMax = size.length;
yinc = yinc || 1;
for(var n = 0; n < nMax; n++) size[n] *= inc[n] * yinc;
},
'probability density': function(size, total, inc, yinc) {
var nMax = size.length;
if(yinc) total /= yinc;
for(var n = 0; n < nMax; n++) size[n] *= inc[n] / total;
}
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 16.67% | (1 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (1 / 6) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) | |
| sample_defaults.js | 21.43% | (3 / 14) | 0% | (0 / 10) | 0% | (0 / 1) | 23.08% | (3 / 13) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 4 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var histogramAttrs = require('../histogram/attributes');
var heatmapAttrs = require('../heatmap/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = extendFlat({},
{
x: histogramAttrs.x,
y: histogramAttrs.y,
z: {
valType: 'data_array',
description: 'Sets the aggregation data.'
},
marker: {
color: {
valType: 'data_array',
description: 'Sets the aggregation data.'
}
},
histnorm: histogramAttrs.histnorm,
histfunc: histogramAttrs.histfunc,
autobinx: histogramAttrs.autobinx,
nbinsx: histogramAttrs.nbinsx,
xbins: histogramAttrs.xbins,
autobiny: histogramAttrs.autobiny,
nbinsy: histogramAttrs.nbinsy,
ybins: histogramAttrs.ybins,
xgap: heatmapAttrs.xgap,
ygap: heatmapAttrs.ygap,
zsmooth: heatmapAttrs.zsmooth
},
colorscaleAttrs,
{ autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale, {dflt: false}) },
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Histogram2D = {};
Histogram2D.attributes = require('./attributes');
Histogram2D.supplyDefaults = require('./defaults');
Histogram2D.calc = require('../heatmap/calc');
Histogram2D.plot = require('../heatmap/plot');
Histogram2D.colorbar = require('../heatmap/colorbar');
Histogram2D.style = require('../heatmap/style');
Histogram2D.hoverPoints = require('../heatmap/hover');
Histogram2D.moduleType = 'trace';
Histogram2D.name = 'histogram2d';
Histogram2D.basePlotModule = require('../../plots/cartesian');
Histogram2D.categories = ['cartesian', '2dMap', 'histogram'];
Histogram2D.meta = {
hrName: 'histogram_2d',
description: [
'The sample data from which statistics are computed is set in `x`',
'and `y` (where `x` and `y` represent marginal distributions,',
'binning is set in `xbins` and `ybins` in this case)',
'or `z` (where `z` represent the 2D distribution and binning set,',
'binning is set by `x` and `y` in this case).',
'The resulting distribution is visualized as a heatmap.'
].join(' ')
};
module.exports = Histogram2D;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var handleBinDefaults = require('../histogram/bin_defaults');
module.exports = function handleSampleDefaults(traceIn, traceOut, coerce, layout) {
var x = coerce('x'),
y = coerce('y');
// we could try to accept x0 and dx, etc...
// but that's a pretty weird use case.
// for now require both x and y explicitly specified.
if(!(x && x.length && y && y.length)) {
traceOut.visible = false;
return;
}
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x', 'y'], layout);
// if marker.color is an array, we can use it in aggregation instead of z
var hasAggregationData = coerce('z') || coerce('marker.color');
if(hasAggregationData) coerce('histfunc');
var binDirections = ['x', 'y'];
handleBinDefaults(traceIn, traceOut, coerce, binDirections);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 16.67% | (1 / 6) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (1 / 6) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var histogram2dAttrs = require('../histogram2d/attributes');
var contourAttrs = require('../contour/attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = extendFlat({}, {
x: histogram2dAttrs.x,
y: histogram2dAttrs.y,
z: histogram2dAttrs.z,
marker: histogram2dAttrs.marker,
histnorm: histogram2dAttrs.histnorm,
histfunc: histogram2dAttrs.histfunc,
autobinx: histogram2dAttrs.autobinx,
nbinsx: histogram2dAttrs.nbinsx,
xbins: histogram2dAttrs.xbins,
autobiny: histogram2dAttrs.autobiny,
nbinsy: histogram2dAttrs.nbinsy,
ybins: histogram2dAttrs.ybins,
autocontour: contourAttrs.autocontour,
ncontours: contourAttrs.ncontours,
contours: contourAttrs.contours,
line: contourAttrs.line
},
colorscaleAttrs,
{ colorbar: colorbarAttrs }
);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Histogram2dContour = {};
Histogram2dContour.attributes = require('./attributes');
Histogram2dContour.supplyDefaults = require('./defaults');
Histogram2dContour.calc = require('../contour/calc');
Histogram2dContour.plot = require('../contour/plot');
Histogram2dContour.style = require('../contour/style');
Histogram2dContour.colorbar = require('../contour/colorbar');
Histogram2dContour.hoverPoints = require('../contour/hover');
Histogram2dContour.moduleType = 'trace';
Histogram2dContour.name = 'histogram2dcontour';
Histogram2dContour.basePlotModule = require('../../plots/cartesian');
Histogram2dContour.categories = ['cartesian', '2dMap', 'contour', 'histogram'];
Histogram2dContour.meta = {
hrName: 'histogram_2d_contour',
description: [
'The sample data from which statistics are computed is set in `x`',
'and `y` (where `x` and `y` represent marginal distributions,',
'binning is set in `xbins` and `ybins` in this case)',
'or `z` (where `z` represent the 2D distribution and binning set,',
'binning is set by `x` and `y` in this case).',
'The resulting distribution is visualized as a contour plot.'
].join(' ')
};
module.exports = Histogram2dContour;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| convert.js | 22.37% | (17 / 76) | 0% | (0 / 17) | 0% | (0 / 12) | 22.37% | (17 / 76) | |
| defaults.js | 16.67% | (7 / 42) | 0% | (0 / 22) | 0% | (0 / 8) | 18.42% | (7 / 38) | |
| index.js | 70% | (7 / 10) | 100% | (0 / 0) | 100% | (0 / 0) | 70% | (7 / 10) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var surfaceAtts = require('../surface/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
x: {
valType: 'data_array',
description: [
'Sets the X coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
'jointly represent the X, Y and Z coordinates of the nth vertex.'
].join(' ')
},
y: {
valType: 'data_array',
description: [
'Sets the Y coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
'jointly represent the X, Y and Z coordinates of the nth vertex.'
].join(' ')
},
z: {
valType: 'data_array',
description: [
'Sets the Z coordinates of the vertices. The nth element of vectors `x`, `y` and `z`',
'jointly represent the X, Y and Z coordinates of the nth vertex.'
].join(' ')
},
i: {
valType: 'data_array',
description: [
'A vector of vertex indices, i.e. integer values between 0 and the length of the vertex',
'vectors, representing the *first* vertex of a triangle. For example, `{i[m], j[m], k[m]}`',
'together represent face m (triangle m) in the mesh, where `i[m] = n` points to the triplet',
'`{x[n], y[n], z[n]}` in the vertex arrays. Therefore, each element in `i` represents a',
'point in space, which is the first vertex of a triangle.'
].join(' ')
},
j: {
valType: 'data_array',
description: [
'A vector of vertex indices, i.e. integer values between 0 and the length of the vertex',
'vectors, representing the *second* vertex of a triangle. For example, `{i[m], j[m], k[m]}` ',
'together represent face m (triangle m) in the mesh, where `j[m] = n` points to the triplet',
'`{x[n], y[n], z[n]}` in the vertex arrays. Therefore, each element in `j` represents a',
'point in space, which is the second vertex of a triangle.'
].join(' ')
},
k: {
valType: 'data_array',
description: [
'A vector of vertex indices, i.e. integer values between 0 and the length of the vertex',
'vectors, representing the *third* vertex of a triangle. For example, `{i[m], j[m], k[m]}`',
'together represent face m (triangle m) in the mesh, where `k[m] = n` points to the triplet ',
'`{x[n], y[n], z[n]}` in the vertex arrays. Therefore, each element in `k` represents a',
'point in space, which is the third vertex of a triangle.'
].join(' ')
},
delaunayaxis: {
valType: 'enumerated',
role: 'info',
values: [ 'x', 'y', 'z' ],
dflt: 'z',
description: [
'Sets the Delaunay axis, which is the axis that is perpendicular to the surface of the',
'Delaunay triangulation.',
'It has an effect if `i`, `j`, `k` are not provided and `alphahull` is set to indicate',
'Delaunay triangulation.'
].join(' ')
},
alphahull: {
valType: 'number',
role: 'style',
dflt: -1,
description: [
'Determines how the mesh surface triangles are derived from the set of',
'vertices (points) represented by the `x`, `y` and `z` arrays, if',
'the `i`, `j`, `k` arrays are not supplied.',
'For general use of `mesh3d` it is preferred that `i`, `j`, `k` are',
'supplied.',
'If *-1*, Delaunay triangulation is used, which is mainly suitable if the',
'mesh is a single, more or less layer surface that is perpendicular to `delaunayaxis`.',
'In case the `delaunayaxis` intersects the mesh surface at more than one point',
'it will result triangles that are very long in the dimension of `delaunayaxis`.',
'If *>0*, the alpha-shape algorithm is used. In this case, the positive `alphahull` value',
'signals the use of the alpha-shape algorithm, _and_ its value',
'acts as the parameter for the mesh fitting.',
'If *0*, the convex-hull algorithm is used. It is suitable for convex bodies',
'or if the intention is to enclose the `x`, `y` and `z` point set into a convex',
'hull.'
].join(' ')
},
intensity: {
valType: 'data_array',
description: [
'Sets the vertex intensity values,',
'used for plotting fields on meshes'
].join(' ')
},
// Color field
color: {
valType: 'color',
role: 'style',
description: 'Sets the color of the whole mesh'
},
vertexcolor: {
valType: 'data_array', // FIXME: this should be a color array
role: 'style',
description: [
'Sets the color of each vertex',
'Overrides *color*.'
].join(' ')
},
facecolor: {
valType: 'data_array',
role: 'style',
description: [
'Sets the color of each face',
'Overrides *color* and *vertexcolor*.'
].join(' ')
},
// Opacity
opacity: extendFlat({}, surfaceAtts.opacity),
// Flat shaded mode
flatshading: {
valType: 'boolean',
role: 'style',
dflt: false,
description: [
'Determines whether or not normal smoothing is applied to the meshes,',
'creating meshes with an angular, low-poly look via flat reflections.'
].join(' ')
},
contour: {
show: extendFlat({}, surfaceAtts.contours.x.show, {
description: [
'Sets whether or not dynamic contours are shown on hover'
].join(' ')
}),
color: extendFlat({}, surfaceAtts.contours.x.color),
width: extendFlat({}, surfaceAtts.contours.x.width)
},
colorscale: colorscaleAttrs.colorscale,
reversescale: colorscaleAttrs.reversescale,
showscale: colorscaleAttrs.showscale,
colorbar: colorbarAttrs,
lightposition: {
'x': extendFlat({}, surfaceAtts.lightposition.x, {dflt: 1e5}),
'y': extendFlat({}, surfaceAtts.lightposition.y, {dflt: 1e5}),
'z': extendFlat({}, surfaceAtts.lightposition.z, {dflt: 0})
},
lighting: extendFlat({}, {
vertexnormalsepsilon: {
valType: 'number',
role: 'style',
min: 0.00,
max: 1,
dflt: 1e-12, // otherwise finely tessellated things eg. the brain will have no specular light reflection
description: 'Epsilon for vertex normals calculation avoids math issues arising from degenerate geometry.'
},
facenormalsepsilon: {
valType: 'number',
role: 'style',
min: 0.00,
max: 1,
dflt: 1e-6, // even the brain model doesn't appear to need finer than this
description: 'Epsilon for face normals calculation avoids math issues arising from degenerate geometry.'
}
}, surfaceAtts.lighting)
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createMesh = require('gl-mesh3d');
var tinycolor = require('tinycolor2');
var triangulate = require('delaunay-triangulate');
var alphaShape = require('alpha-shape');
var convexHull = require('convex-hull');
var str2RgbaArray = require('../../lib/str2rgbarray');
function Mesh3DTrace(scene, mesh, uid) {
this.scene = scene;
this.uid = uid;
this.mesh = mesh;
this.name = '';
this.color = '#fff';
this.data = null;
this.showContour = false;
}
var proto = Mesh3DTrace.prototype;
proto.handlePick = function(selection) {
if(selection.object === this.mesh) {
var selectIndex = selection.data.index;
selection.traceCoordinate = [
this.data.x[selectIndex],
this.data.y[selectIndex],
this.data.z[selectIndex]
];
return true;
}
};
function parseColorScale(colorscale) {
return colorscale.map(function(elem) {
var index = elem[0];
var color = tinycolor(elem[1]);
var rgb = color.toRgb();
return {
index: index,
rgb: [rgb.r, rgb.g, rgb.b, 1]
};
});
}
function parseColorArray(colors) {
return colors.map(str2RgbaArray);
}
function zip3(x, y, z) {
var result = new Array(x.length);
for(var i = 0; i < x.length; ++i) {
result[i] = [x[i], y[i], z[i]];
}
return result;
}
proto.update = function(data) {
var scene = this.scene,
layout = scene.fullSceneLayout;
this.data = data;
// Unpack position data
function toDataCoords(axis, coord, scale, calendar) {
return coord.map(function(x) {
return axis.d2l(x, 0, calendar) * scale;
});
}
var positions = zip3(
toDataCoords(layout.xaxis, data.x, scene.dataScale[0], data.xcalendar),
toDataCoords(layout.yaxis, data.y, scene.dataScale[1], data.ycalendar),
toDataCoords(layout.zaxis, data.z, scene.dataScale[2], data.zcalendar));
var cells;
if(data.i && data.j && data.k) {
cells = zip3(data.i, data.j, data.k);
}
else if(data.alphahull === 0) {
cells = convexHull(positions);
}
else if(data.alphahull > 0) {
cells = alphaShape(data.alphahull, positions);
}
else {
var d = ['x', 'y', 'z'].indexOf(data.delaunayaxis);
cells = triangulate(positions.map(function(c) {
return [c[(d + 1) % 3], c[(d + 2) % 3]];
}));
}
var config = {
positions: positions,
cells: cells,
lightPosition: [data.lightposition.x, data.lightposition.y, data.lightposition.z],
ambient: data.lighting.ambient,
diffuse: data.lighting.diffuse,
specular: data.lighting.specular,
roughness: data.lighting.roughness,
fresnel: data.lighting.fresnel,
vertexNormalsEpsilon: data.lighting.vertexnormalsepsilon,
faceNormalsEpsilon: data.lighting.facenormalsepsilon,
opacity: data.opacity,
contourEnable: data.contour.show,
contourColor: str2RgbaArray(data.contour.color).slice(0, 3),
contourWidth: data.contour.width,
useFacetNormals: data.flatshading
};
if(data.intensity) {
this.color = '#fff';
config.vertexIntensity = data.intensity;
config.colormap = parseColorScale(data.colorscale);
}
else if(data.vertexcolor) {
this.color = data.vertexcolors[0];
config.vertexColors = parseColorArray(data.vertexcolor);
}
else if(data.facecolor) {
this.color = data.facecolor[0];
config.cellColors = parseColorArray(data.facecolor);
}
else {
this.color = data.color;
config.meshColor = str2RgbaArray(data.color);
}
// Update mesh
this.mesh.update(config);
};
proto.dispose = function() {
this.scene.glplot.remove(this.mesh);
this.mesh.dispose();
};
function createMesh3DTrace(scene, data) {
var gl = scene.glplot.gl;
var mesh = createMesh({gl: gl});
var result = new Mesh3DTrace(scene, mesh, data.uid);
mesh._trace = result;
result.update(data);
scene.glplot.add(mesh);
return result;
}
module.exports = createMesh3DTrace;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var colorbarDefaults = require('../../components/colorbar/defaults');
var attributes = require('./attributes');
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}
// read in face/vertex properties
function readComponents(array) {
var ret = array.map(function(attr) {
var result = coerce(attr);
if(result && Array.isArray(result)) return result;
return null;
});
return ret.every(function(x) {
return x && x.length === ret[0].length;
}) && ret;
}
var coords = readComponents(['x', 'y', 'z']);
var indices = readComponents(['i', 'j', 'k']);
if(!coords) {
traceOut.visible = false;
return;
}
if(indices) {
// otherwise, convert all face indices to ints
indices.forEach(function(index) {
for(var i = 0; i < index.length; ++i) index[i] |= 0;
});
}
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);
// Coerce remaining properties
[
'lighting.ambient',
'lighting.diffuse',
'lighting.specular',
'lighting.roughness',
'lighting.fresnel',
'lighting.vertexnormalsepsilon',
'lighting.facenormalsepsilon',
'lightposition.x',
'lightposition.y',
'lightposition.z',
'contour.show',
'contour.color',
'contour.width',
'colorscale',
'reversescale',
'flatshading',
'alphahull',
'delaunayaxis',
'opacity'
].forEach(function(x) { coerce(x); });
if('intensity' in traceIn) {
coerce('intensity');
coerce('showscale', true);
}
else {
traceOut.showscale = false;
if('vertexcolor' in traceIn) coerce('vertexcolor');
else if('facecolor' in traceIn) coerce('facecolor');
else coerce('color', defaultColor);
}
if(traceOut.reversescale) {
traceOut.colorscale = traceOut.colorscale.map(function(si) {
return [1 - si[0], si[1]];
}).reverse();
}
if(traceOut.showscale) {
colorbarDefaults(traceIn, traceOut, layout);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 2 2 2 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Mesh3D = {};
Mesh3D.attributes = require('./attributes');
Mesh3D.supplyDefaults = require('./defaults');
Mesh3D.colorbar = require('../heatmap/colorbar');
Mesh3D.plot = require('./convert');
Mesh3D.moduleType = 'trace';
Mesh3D.name = 'mesh3d',
Mesh3D.basePlotModule = require('../../plots/gl3d');
Mesh3D.categories = ['gl3d'];
Mesh3D.meta = {
description: [
'Draws sets of triangles with coordinates given by',
'three 1-dimensional arrays in `x`, `y`, `z` and',
'(1) a sets of `i`, `j`, `k` indices',
'(2) Delaunay triangulation or',
'(3) the Alpha-shape algorithm or',
'(4) the Convex-hull algorithm'
].join(' ')
};
module.exports = Mesh3D;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 25% | (2 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 25% | (2 / 8) | |
| direction_defaults.js | 16.67% | (1 / 6) | 0% | (0 / 2) | 0% | (0 / 1) | 16.67% | (1 / 6) | |
| helpers.js | 15.56% | (7 / 45) | 0% | (0 / 26) | 0% | (0 / 8) | 20.59% | (7 / 34) | |
| index.js | 50% | (2 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 50% | (2 / 4) | |
| ohlc_defaults.js | 10% | (2 / 20) | 0% | (0 / 12) | 0% | (0 / 1) | 13.33% | (2 / 15) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 4 4 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var scatterAttrs = require('../scatter/attributes');
var dash = require('../../components/drawing/attributes').dash;
var INCREASING_COLOR = '#3D9970';
var DECREASING_COLOR = '#FF4136';
var lineAttrs = scatterAttrs.line;
var directionAttrs = {
name: {
valType: 'string',
role: 'info',
description: [
'Sets the segment name.',
'The segment name appear as the legend item and on hover.'
].join(' ')
},
showlegend: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not an item corresponding to this',
'segment is shown in the legend.'
].join(' ')
},
line: {
color: lineAttrs.color,
width: lineAttrs.width,
dash: dash,
}
};
module.exports = {
x: {
valType: 'data_array',
description: [
'Sets the x coordinates.',
'If absent, linear coordinate will be generated.'
].join(' ')
},
open: {
valType: 'data_array',
dflt: [],
description: 'Sets the open values.'
},
high: {
valType: 'data_array',
dflt: [],
description: 'Sets the high values.'
},
low: {
valType: 'data_array',
dflt: [],
description: 'Sets the low values.'
},
close: {
valType: 'data_array',
dflt: [],
description: 'Sets the close values.'
},
line: {
width: Lib.extendFlat({}, lineAttrs.width, {
description: [
lineAttrs.width,
'Note that this style setting can also be set per',
'direction via `increasing.line.width` and',
'`decreasing.line.width`.'
].join(' ')
}),
dash: Lib.extendFlat({}, dash, {
description: [
dash.description,
'Note that this style setting can also be set per',
'direction via `increasing.line.dash` and',
'`decreasing.line.dash`.'
].join(' ')
}),
},
increasing: Lib.extendDeep({}, directionAttrs, {
line: { color: { dflt: INCREASING_COLOR } }
}),
decreasing: Lib.extendDeep({}, directionAttrs, {
line: { color: { dflt: DECREASING_COLOR } }
}),
text: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets hover text elements associated with each sample point.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to',
'this trace\'s sample points.'
].join(' ')
},
tickwidth: {
valType: 'number',
min: 0,
max: 0.5,
dflt: 0.3,
role: 'style',
description: [
'Sets the width of the open/close tick marks',
'relative to the *x* minimal interval.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function handleDirectionDefaults(traceIn, traceOut, coerce, direction) { coerce(direction + '.showlegend'); // trace-wide *showlegend* overrides direction *showlegend* if(traceIn.showlegend === false) { traceOut[direction].showlegend = false; } var nameDflt = traceOut.name + ' - ' + direction; coerce(direction + '.name', nameDflt); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
// This routine gets called during the trace supply-defaults step.
//
// This is a hacky way to make 'ohlc' and 'candlestick' trace types
// go through the transform machinery.
//
// Note that, we must mutate user data (here traceIn) as opposed
// to full data (here traceOut) as - at the moment - transform
// defaults (which are called after trace defaults) start
// from a clear transforms container. The mutations inflicted are
// cleared in exports.clearEphemeralTransformOpts.
exports.pushDummyTransformOpts = function(traceIn, traceOut) {
var transformOpts = {
// give dummy transform the same type as trace
type: traceOut.type,
// track ephemeral transforms in user data
_ephemeral: true
};
if(Array.isArray(traceIn.transforms)) {
traceIn.transforms.push(transformOpts);
}
else {
traceIn.transforms = [transformOpts];
}
};
// This routine gets called during the transform supply-defaults step
// where it clears ephemeral transform opts in user data
// and effectively put back user date in its pre-supplyDefaults state.
exports.clearEphemeralTransformOpts = function(traceIn) {
var transformsIn = traceIn.transforms;
if(!Array.isArray(transformsIn)) return;
for(var i = 0; i < transformsIn.length; i++) {
if(transformsIn[i]._ephemeral) transformsIn.splice(i, 1);
}
if(transformsIn.length === 0) delete traceIn.transforms;
};
// This routine gets called during the transform supply-defaults step
// where it passes 'ohlc' and 'candlestick' attributes
// (found the transform container via exports.makeTransform)
// to the traceOut container such that they can
// be compatible with filter and groupby transforms.
//
// Note that this routine only has an effect during the
// second round of transform defaults done on generated traces
exports.copyOHLC = function(container, traceOut) {
if(container.open) traceOut.open = container.open;
if(container.high) traceOut.high = container.high;
if(container.low) traceOut.low = container.low;
if(container.close) traceOut.close = container.close;
};
// This routine gets called during the applyTransform step.
//
// We need to track trace attributes and which direction
// ('increasing' or 'decreasing')
// the generated correspond to for the calcTransform step.
//
// To make sure that the attributes reach the calcTransform,
// store it in the transform opts object.
exports.makeTransform = function(traceIn, state, direction) {
var out = Lib.extendFlat([], traceIn.transforms);
out[state.transformIndex] = {
type: traceIn.type,
direction: direction,
// these are copied to traceOut during exports.copyOHLC
open: traceIn.open,
high: traceIn.high,
low: traceIn.low,
close: traceIn.close
};
return out;
};
exports.getFilterFn = function(direction) {
switch(direction) {
case 'increasing':
return function(o, c) { return o <= c; };
case 'decreasing':
return function(o, c) { return o > c; };
}
};
exports.addRangeSlider = function(data, layout) {
var hasOneVisibleTrace = false;
for(var i = 0; i < data.length; i++) {
if(data[i].visible === true) {
hasOneVisibleTrace = true;
break;
}
}
if(hasOneVisibleTrace) {
if(!layout.xaxis) layout.xaxis = {};
if(!layout.xaxis.rangeslider) layout.xaxis.rangeslider = {};
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var register = require('../../plot_api/register');
module.exports = {
moduleType: 'trace',
name: 'ohlc',
basePlotModule: require('../../plots/cartesian'),
categories: ['cartesian', 'showLegend'],
meta: {
description: [
'The ohlc (short for Open-High-Low-Close) is a style of financial chart describing',
'open, high, low and close for a given `x` coordinate (most likely time).',
'The tip of the lines represent the `low` and `high` values and',
'the horizontal segments represent the `open` and `close` values.',
'Sample points where the close value is higher (lower) then the open',
'value are called increasing (decreasing).',
'By default, increasing candles are drawn in green whereas',
'decreasing are drawn in red.'
].join(' ')
},
attributes: require('./attributes'),
supplyDefaults: require('./defaults'),
};
register(require('../scatter'));
register(require('./transform'));
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
module.exports = function handleOHLC(traceIn, traceOut, coerce, layout) {
var len;
var x = coerce('x'),
open = coerce('open'),
high = coerce('high'),
low = coerce('low'),
close = coerce('close');
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x'], layout);
len = Math.min(open.length, high.length, low.length, close.length);
if(x) {
len = Math.min(len, x.length);
if(len < x.length) traceOut.x = x.slice(0, len);
}
if(len < open.length) traceOut.open = open.slice(0, len);
if(len < high.length) traceOut.high = high.slice(0, len);
if(len < low.length) traceOut.low = low.slice(0, len);
if(len < close.length) traceOut.close = close.slice(0, len);
return len;
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (7 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (7 / 7) | |
| base_plot.js | 31.43% | (11 / 35) | 0% | (0 / 10) | 0% | (0 / 6) | 33.33% | (11 / 33) | |
| constants.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| index.js | 16.67% | (2 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (2 / 12) | |
| lines.js | 19.61% | (30 / 153) | 0% | (0 / 62) | 0% | (0 / 25) | 20.41% | (30 / 147) | |
| parcoords.js | 7.88% | (29 / 368) | 0% | (0 / 153) | 0% | (0 / 89) | 8.41% | (29 / 345) | |
| plot.js | 10.87% | (5 / 46) | 0% | (0 / 8) | 0% | (0 / 13) | 11.36% | (5 / 44) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 2 2 2 2 2 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorAttributes = require('../../components/colorscale/color_attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var colorscales = require('../../components/colorscale/scales');
var axesAttrs = require('../../plots/cartesian/layout_attributes');
var extendDeep = require('../../lib/extend').extendDeep;
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
domain: {
x: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1},
{valType: 'number', min: 0, max: 1}
],
dflt: [0, 1],
description: [
'Sets the horizontal domain of this `parcoords` trace',
'(in plot fraction).'
].join(' ')
},
y: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1},
{valType: 'number', min: 0, max: 1}
],
dflt: [0, 1],
description: [
'Sets the vertical domain of this `parcoords` trace',
'(in plot fraction).'
].join(' ')
}
},
dimensions: {
_isLinkedToArray: 'dimension',
label: {
valType: 'string',
role: 'info',
description: 'The shown name of the dimension.'
},
tickvals: axesAttrs.tickvals,
ticktext: axesAttrs.ticktext,
tickformat: {
valType: 'string',
dflt: '3s',
role: 'style',
description: [
'Sets the tick label formatting rule using d3 formatting mini-language',
'which is similar to those of Python. See',
'https://github.com/d3/d3-format/blob/master/README.md#locale_format'
].join(' ')
},
visible: {
valType: 'boolean',
dflt: true,
role: 'info',
description: 'Shows the dimension when set to `true` (the default). Hides the dimension for `false`.'
},
range: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number'},
{valType: 'number'}
],
description: [
'The domain range that represents the full, shown axis extent. Defaults to the `values` extent.',
'Must be an array of `[fromValue, toValue]` with finite numbers as elements.'
].join(' ')
},
constraintrange: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number'},
{valType: 'number'}
],
description: [
'The domain range to which the filter on the dimension is constrained. Must be an array',
'of `[fromValue, toValue]` with finite numbers as elements.'
].join(' ')
},
values: {
valType: 'data_array',
role: 'info',
dflt: [],
description: [
'Dimension values. `values[n]` represents the value of the `n`th point in the dataset,',
'therefore the `values` vector for all dimensions must be the same (longer vectors',
'will be truncated). Each value must be a finite number.'
].join(' ')
},
description: 'The dimensions (variables) of the parallel coordinates chart. 2..60 dimensions are supported.'
},
line: extendFlat({},
// the default autocolorscale isn't quite usable for parcoords due to context ambiguity around 0 (grey, off-white)
// autocolorscale therefore defaults to false too, to avoid being overridden by the blue-white-red autocolor palette
extendDeep(
{},
colorAttributes('line'),
{
colorscale: extendDeep(
{},
colorAttributes('line').colorscale,
{dflt: colorscales.Viridis}
),
autocolorscale: extendDeep(
{},
colorAttributes('line').autocolorscale,
{
dflt: false,
description: [
'Has an effect only if line.color` is set to a numerical array.',
'Determines whether the colorscale is a default palette (`autocolorscale: true`)',
'or the palette determined by `line.colorscale`.',
'In case `colorscale` is unspecified or `autocolorscale` is true, the default ',
'palette will be chosen according to whether numbers in the `color` array are',
'all positive, all negative or mixed.',
'The default value is false, so that `parcoords` colorscale can default to `Viridis`.'
].join(' ')
}
)
}
),
{
showscale: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Has an effect only if `line.color` is set to a numerical array.',
'Determines whether or not a colorbar is displayed.'
].join(' ')
},
colorbar: colorbarAttrs
}
)
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Plots = require('../../plots/plots');
var parcoordsPlot = require('./plot');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
var c = require('./constants');
exports.name = 'parcoords';
exports.attr = 'type';
exports.plot = function(gd) {
var calcData = Plots.getSubplotCalcData(gd.calcdata, 'parcoords', 'parcoords');
if(calcData.length) parcoordsPlot(gd, calcData);
};
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var hadParcoords = (oldFullLayout._has && oldFullLayout._has('parcoords'));
var hasParcoords = (newFullLayout._has && newFullLayout._has('parcoords'));
if(hadParcoords && !hasParcoords) {
oldFullLayout._paperdiv.selectAll('.parcoords-line-layers').remove();
oldFullLayout._paperdiv.selectAll('.parcoords-line-layers').remove();
oldFullLayout._paperdiv.selectAll('.parcoords').remove();
oldFullLayout._paperdiv.selectAll('.parcoords').remove();
oldFullLayout._glimages.selectAll('*').remove();
}
};
exports.toSVG = function(gd) {
var imageRoot = gd._fullLayout._glimages;
var root = d3.selectAll('.svg-container');
var canvases = root.filter(function(d, i) {return i === root.size() - 1;})
.selectAll('.parcoords-lines.context, .parcoords-lines.focus');
function canvasToImage(d) {
var canvas = this;
var imageData = canvas.toDataURL('image/png');
var image = imageRoot.append('svg:image');
var size = gd._fullLayout._size;
var domain = gd._fullData[d.model.key].domain;
image.attr({
xmlns: xmlnsNamespaces.svg,
'xlink:href': imageData,
x: size.l + size.w * domain.x[0] - c.overdrag,
y: size.t + size.h * (1 - domain.y[1]),
width: (domain.x[1] - domain.x[0]) * size.w + 2 * c.overdrag,
height: (domain.y[1] - domain.y[0]) * size.h,
preserveAspectRatio: 'none'
});
}
canvases.each(canvasToImage);
// Chrome / Safari bug workaround - browser apparently loses connection to the defined pattern
// Without the workaround, these browsers 'lose' the filter brush styling (color etc.) after a snapshot
// on a subsequent interaction.
// Firefox works fine without this workaround
window.setTimeout(function() {
d3.selectAll('#filterBarPattern')
.attr('id', 'filterBarPattern');
}, 60);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
maxDimensionCount: 60, // this cannot be increased without WebGL code refactoring
overdrag: 45,
verticalPadding: 2, // otherwise, horizontal lines on top or bottom are of lower width
tickDistance: 50,
canvasPixelRatio: 1,
blockLineCount: 5000,
scatter: false,
layers: ['contextLineLayer', 'focusLineLayer', 'pickLineLayer'],
axisTitleOffset: 28,
axisExtentOffset: 10,
bar: {
width: 4, // Visible width of the filter bar
capturewidth: 10, // Mouse-sensitive width for interaction (Fitts law)
fillcolor: 'magenta', // Color of the filter bar fill
fillopacity: 1, // Filter bar fill opacity
strokecolor: 'white', // Color of the filter bar side lines
strokeopacity: 1, // Filter bar side stroke opacity
strokewidth: 1, // Filter bar side stroke width in pixels
handleheight: 16, // Height of the filter bar vertical resize areas on top and bottom
handleopacity: 1, // Opacity of the filter bar vertical resize areas on top and bottom
handleoverlap: 0 // A larger than 0 value causes overlaps with the filter bar, represented as pixels.'
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Parcoords = {};
Parcoords.attributes = require('./attributes');
Parcoords.supplyDefaults = require('./defaults');
Parcoords.calc = require('./calc');
Parcoords.plot = require('./plot');
Parcoords.colorbar = require('./colorbar');
Parcoords.moduleType = 'trace';
Parcoords.name = 'parcoords';
Parcoords.basePlotModule = require('./base_plot');
Parcoords.categories = ['gl', 'noOpacity'];
Parcoords.meta = {
description: [
'Parallel coordinates for multidimensional exploratory data analysis.',
'The samples are specified in `dimensions`.',
'The colors are set in `line.color`.'
].join(' ')
};
module.exports = Parcoords;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createREGL = require('regl');
var glslify = require('glslify');
var verticalPadding = require('./constants').verticalPadding;
var vertexShaderSource = glslify('./shaders/vertex.glsl');
var pickVertexShaderSource = glslify('./shaders/pick_vertex.glsl');
var fragmentShaderSource = glslify('./shaders/fragment.glsl');
var depthLimitEpsilon = 1e-6; // don't change; otherwise near/far plane lines are lost
var gpuDimensionCount = 64;
var sectionVertexCount = 2;
var vec4NumberCount = 4;
var contextColor = [119, 119, 119]; // middle gray to not drawn the focus; looks good on a black or white background
var dummyPixel = new Uint8Array(4);
var pickPixel = new Uint8Array(4);
function ensureDraw(regl) {
regl.read({
x: 0,
y: 0,
width: 1,
height: 1,
data: dummyPixel
});
}
function clear(regl, x, y, width, height) {
var gl = regl._gl;
gl.enable(gl.SCISSOR_TEST);
gl.scissor(x, y, width, height);
regl.clear({color: [0, 0, 0, 0], depth: 1}); // clearing is done in scissored panel only
}
function renderBlock(regl, glAes, renderState, blockLineCount, sampleCount, item) {
var rafKey = item.key;
function render(blockNumber) {
var count;
count = Math.min(blockLineCount, sampleCount - blockNumber * blockLineCount);
item.offset = sectionVertexCount * blockNumber * blockLineCount;
item.count = sectionVertexCount * count;
if(blockNumber === 0) {
window.cancelAnimationFrame(renderState.currentRafs[rafKey]); // stop drawing possibly stale glyphs before clearing
delete renderState.currentRafs[rafKey];
clear(regl, item.scissorX, item.scissorY, item.scissorWidth, item.viewBoxSize[1]);
}
if(renderState.clearOnly) {
return;
}
glAes(item);
if(blockNumber * blockLineCount + count < sampleCount) {
renderState.currentRafs[rafKey] = window.requestAnimationFrame(function() {
render(blockNumber + 1);
});
}
renderState.drawCompleted = false;
}
if(!renderState.drawCompleted) {
ensureDraw(regl);
renderState.drawCompleted = true;
}
// start with rendering item 0; recursion handles the rest
render(0);
}
function adjustDepth(d) {
// WebGL matrix operations use floats with limited precision, potentially causing a number near a border of [0, 1]
// to end up slightly outside the border. With an epsilon, we reduce the chance that a line gets clipped by the
// near or the far plane.
return Math.max(depthLimitEpsilon, Math.min(1 - depthLimitEpsilon, d));
}
function palette(unitToColor, context, opacity) {
var result = [];
for(var j = 0; j < 256; j++) {
var c = unitToColor(j / 255);
result.push((context ? contextColor : c).concat(opacity));
}
return result;
}
// Maps the sample index [0...sampleCount - 1] to a range of [0, 1] as the shader expects colors in the [0, 1] range.
// but first it shifts the sample index by 0, 8 or 16 bits depending on rgbIndex [0..2]
// with the end result that each line will be of a unique color, making it possible for the pick handler
// to uniquely identify which line is hovered over (bijective mapping).
// The inverse, i.e. readPixel is invoked from 'parcoords.js'
function calcPickColor(j, rgbIndex) {
return (j >>> 8 * rgbIndex) % 256 / 255;
}
function makePoints(sampleCount, dimensionCount, dimensions, color) {
var points = [];
for(var j = 0; j < sampleCount; j++) {
for(var i = 0; i < gpuDimensionCount; i++) {
points.push(i < dimensionCount ?
dimensions[i].paddedUnitValues[j] :
i === (gpuDimensionCount - 1) ?
adjustDepth(color[j]) :
i >= gpuDimensionCount - 4 ?
calcPickColor(j, gpuDimensionCount - 2 - i) :
0.5);
}
}
return points;
}
function makeVecAttr(sampleCount, points, vecIndex) {
var i, j, k;
var pointPairs = [];
for(j = 0; j < sampleCount; j++) {
for(k = 0; k < sectionVertexCount; k++) {
for(i = 0; i < vec4NumberCount; i++) {
pointPairs.push(points[j * gpuDimensionCount + vecIndex * vec4NumberCount + i]);
if(vecIndex * vec4NumberCount + i === gpuDimensionCount - 1 && k % 2 === 0) {
pointPairs[pointPairs.length - 1] *= -1;
}
}
}
}
return pointPairs;
}
function makeAttributes(sampleCount, points) {
var vecIndices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var vectors = vecIndices.map(function(vecIndex) {return makeVecAttr(sampleCount, points, vecIndex);});
var attributes = {};
vectors.forEach(function(v, vecIndex) {
attributes['p' + vecIndex.toString(16)] = v;
});
return attributes;
}
function valid(i, offset, panelCount) {
return i + offset <= panelCount;
}
module.exports = function(canvasGL, lines, canvasWidth, canvasHeight, initialDimensions, initialPanels, unitToColor, context, pick, scatter) {
var renderState = {
currentRafs: {},
drawCompleted: true,
clearOnly: false
};
var initialDims = initialDimensions.slice();
var dimensionCount = initialDims.length;
var sampleCount = initialDims[0] ? initialDims[0].values.length : 0;
var focusAlphaBlending = context;
var color = pick ? lines.color.map(function(_, i) {return i / lines.color.length;}) : lines.color;
var contextOpacity = Math.max(1 / 255, Math.pow(1 / color.length, 1 / 3));
var overdrag = lines.canvasOverdrag;
var panelCount = initialPanels.length;
var points = makePoints(sampleCount, dimensionCount, initialDims, color);
var attributes = makeAttributes(sampleCount, points);
var regl = createREGL({
canvas: canvasGL,
attributes: {
preserveDrawingBuffer: true,
antialias: !pick
}
});
var paletteTexture = regl.texture({
shape: [256, 1],
format: 'rgba',
type: 'uint8',
mag: 'nearest',
min: 'nearest',
data: palette(unitToColor, context, Math.round((context ? contextOpacity : 1) * 255))
});
var glAes = regl({
profile: false,
blend: {
enable: focusAlphaBlending,
func: {
srcRGB: 'src alpha',
dstRGB: 'one minus src alpha',
srcAlpha: 1,
dstAlpha: 1 // 'one minus src alpha'
},
equation: {
rgb: 'add',
alpha: 'add'
},
color: [0, 0, 0, 0]
},
depth: {
enable: !focusAlphaBlending,
mask: true,
func: 'less',
range: [0, 1]
},
// for polygons
cull: {
enable: true,
face: 'back'
},
scissor: {
enable: true,
box: {
x: regl.prop('scissorX'),
y: regl.prop('scissorY'),
width: regl.prop('scissorWidth'),
height: regl.prop('scissorHeight')
}
},
dither: false,
vert: pick ? pickVertexShaderSource : vertexShaderSource,
frag: fragmentShaderSource,
primitive: 'lines',
lineWidth: 1,
attributes: attributes,
uniforms: {
resolution: regl.prop('resolution'),
viewBoxPosition: regl.prop('viewBoxPosition'),
viewBoxSize: regl.prop('viewBoxSize'),
dim1A: regl.prop('dim1A'),
dim2A: regl.prop('dim2A'),
dim1B: regl.prop('dim1B'),
dim2B: regl.prop('dim2B'),
dim1C: regl.prop('dim1C'),
dim2C: regl.prop('dim2C'),
dim1D: regl.prop('dim1D'),
dim2D: regl.prop('dim2D'),
loA: regl.prop('loA'),
hiA: regl.prop('hiA'),
loB: regl.prop('loB'),
hiB: regl.prop('hiB'),
loC: regl.prop('loC'),
hiC: regl.prop('hiC'),
loD: regl.prop('loD'),
hiD: regl.prop('hiD'),
palette: paletteTexture,
colorClamp: regl.prop('colorClamp'),
scatter: regl.prop('scatter')
},
offset: regl.prop('offset'),
count: regl.prop('count')
});
var colorClamp = [0, 1];
function setColorDomain(unitDomain) {
colorClamp[0] = unitDomain[0];
colorClamp[1] = unitDomain[1];
}
var previousAxisOrder = [];
function makeItem(i, ii, x, y, panelSizeX, canvasPanelSizeY, crossfilterDimensionIndex, scatter, I, leftmost, rightmost) {
var loHi, abcd, d, index;
var leftRight = [i, ii];
var filterEpsilon = verticalPadding / canvasPanelSizeY;
var dims = [0, 1].map(function() {return [0, 1, 2, 3].map(function() {return new Float32Array(16);});});
var lims = [0, 1].map(function() {return [0, 1, 2, 3].map(function() {return new Float32Array(16);});});
for(loHi = 0; loHi < 2; loHi++) {
index = leftRight[loHi];
for(abcd = 0; abcd < 4; abcd++) {
for(d = 0; d < 16; d++) {
var dimP = d + 16 * abcd;
dims[loHi][abcd][d] = d + 16 * abcd === index ? 1 : 0;
lims[loHi][abcd][d] = (!context && valid(d, 16 * abcd, panelCount) ? initialDims[dimP === 0 ? 0 : 1 + ((dimP - 1) % (initialDims.length - 1))].filter[loHi] : loHi) + (2 * loHi - 1) * filterEpsilon;
}
}
}
return {
key: crossfilterDimensionIndex,
resolution: [canvasWidth, canvasHeight],
viewBoxPosition: [x + overdrag, y],
viewBoxSize: [panelSizeX, canvasPanelSizeY],
i: i,
ii: ii,
dim1A: dims[0][0],
dim1B: dims[0][1],
dim1C: dims[0][2],
dim1D: dims[0][3],
dim2A: dims[1][0],
dim2B: dims[1][1],
dim2C: dims[1][2],
dim2D: dims[1][3],
loA: lims[0][0],
loB: lims[0][1],
loC: lims[0][2],
loD: lims[0][3],
hiA: lims[1][0],
hiB: lims[1][1],
hiC: lims[1][2],
hiD: lims[1][3],
colorClamp: colorClamp,
scatter: scatter || 0,
scissorX: I === leftmost ? 0 : x + overdrag,
scissorWidth: (I === rightmost ? canvasWidth - x + overdrag : panelSizeX + 0.5) + (I === leftmost ? x + overdrag : 0),
scissorY: y,
scissorHeight: canvasPanelSizeY
};
}
function renderGLParcoords(panels, setChanged, clearOnly) {
var I;
var leftmost, rightmost, lowestX = Infinity, highestX = -Infinity;
for(I = 0; I < panelCount; I++) {
if(panels[I].dim2.canvasX > highestX) {
highestX = panels[I].dim2.canvasX;
rightmost = I;
}
if(panels[I].dim1.canvasX < lowestX) {
lowestX = panels[I].dim1.canvasX;
leftmost = I;
}
}
if(panelCount === 0) {
// clear canvas here, as the panel iteration below will not enter the loop body
clear(regl, 0, 0, canvasWidth, canvasHeight);
}
for(I = 0; I < panelCount; I++) {
var panel = panels[I];
var dim1 = panel.dim1;
var i = dim1.crossfilterDimensionIndex;
var x = panel.canvasX;
var y = panel.canvasY;
var dim2 = panel.dim2;
var ii = dim2.crossfilterDimensionIndex;
var panelSizeX = panel.panelSizeX;
var panelSizeY = panel.panelSizeY;
var xTo = x + panelSizeX;
if(setChanged || !previousAxisOrder[i] || previousAxisOrder[i][0] !== x || previousAxisOrder[i][1] !== xTo) {
previousAxisOrder[i] = [x, xTo];
var item = makeItem(i, ii, x, y, panelSizeX, panelSizeY, dim1.crossfilterDimensionIndex, scatter || dim1.scatter ? 1 : 0, I, leftmost, rightmost);
renderState.clearOnly = clearOnly;
renderBlock(regl, glAes, renderState, setChanged ? lines.blockLineCount : sampleCount, sampleCount, item);
}
}
}
function readPixel(canvasX, canvasY) {
regl.read({
x: canvasX,
y: canvasY,
width: 1,
height: 1,
data: pickPixel
});
return pickPixel;
}
function readPixels(canvasX, canvasY, width, height) {
var pixelArray = new Uint8Array(4 * width * height);
regl.read({
x: canvasX,
y: canvasY,
width: width,
height: height,
data: pixelArray
});
return pixelArray;
}
return {
setColorDomain: setColorDomain,
render: renderGLParcoords,
readPixel: readPixel,
readPixels: readPixels,
destroy: regl.destroy
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var lineLayerMaker = require('./lines');
var c = require('./constants');
var Lib = require('../../lib');
var d3 = require('d3');
function keyFun(d) {return d.key;}
function repeat(d) {return [d];}
function visible(dimension) {return !('visible' in dimension) || dimension.visible;}
function dimensionExtent(dimension) {
var lo = dimension.range ? dimension.range[0] : d3.min(dimension.values);
var hi = dimension.range ? dimension.range[1] : d3.max(dimension.values);
if(isNaN(lo) || !isFinite(lo)) {
lo = 0;
}
if(isNaN(hi) || !isFinite(hi)) {
hi = 0;
}
// avoid a degenerate (zero-width) domain
if(lo === hi) {
if(lo === void(0)) {
lo = 0;
hi = 1;
} else if(lo === 0) {
// no use to multiplying zero, so add/subtract in this case
lo -= 1;
hi += 1;
} else {
// this keeps the range in the order of magnitude of the data
lo *= 0.9;
hi *= 1.1;
}
}
return [lo, hi];
}
function ordinalScaleSnap(scale, v) {
var i, a, prevDiff, prevValue, diff;
for(i = 0, a = scale.range(), prevDiff = Infinity, prevValue = a[0], diff; i < a.length; i++) {
if((diff = Math.abs(a[i] - v)) > prevDiff) {
return prevValue;
}
prevDiff = diff;
prevValue = a[i];
}
return a[a.length - 1];
}
function domainScale(height, padding, dimension) {
var extent = dimensionExtent(dimension);
return dimension.tickvals ?
d3.scale.ordinal()
.domain(dimension.tickvals)
.range(dimension.tickvals
.map(function(d) {return (d - extent[0]) / (extent[1] - extent[0]);})
.map(function(d) {return (height - padding + d * (padding - (height - padding)));})) :
d3.scale.linear()
.domain(extent)
.range([height - padding, padding]);
}
function unitScale(height, padding) {return d3.scale.linear().range([height - padding, padding]);}
function domainToUnitScale(dimension) {return d3.scale.linear().domain(dimensionExtent(dimension));}
function ordinalScale(dimension) {
var extent = dimensionExtent(dimension);
return dimension.tickvals && d3.scale.ordinal()
.domain(dimension.tickvals)
.range(dimension.tickvals.map(function(d) {return (d - extent[0]) / (extent[1] - extent[0]);}));
}
function unitToColorScale(cscale) {
var colorStops = cscale.map(function(d) {return d[0];});
var colorStrings = cscale.map(function(d) {return d[1];});
var colorTuples = colorStrings.map(function(c) {return d3.rgb(c);});
var prop = function(n) {return function(o) {return o[n];};};
// We can't use d3 color interpolation as we may have non-uniform color palette raster
// (various color stop distances).
var polylinearUnitScales = 'rgb'.split('').map(function(key) {
return d3.scale.linear()
.clamp(true)
.domain(colorStops)
.range(colorTuples.map(prop(key)));
});
return function(d) {
return polylinearUnitScales.map(function(s) {
return s(d);
});
};
}
function unwrap(d) {
return d[0]; // plotly data structure convention
}
function model(layout, d, i) {
var cd0 = unwrap(d),
trace = cd0.trace,
lineColor = cd0.lineColor,
cscale = cd0.cscale,
line = trace.line,
domain = trace.domain,
dimensions = trace.dimensions,
width = layout.width;
var lines = Lib.extendDeep({}, line, {
color: lineColor.map(domainToUnitScale({values: lineColor, range: [line.cmin, line.cmax]})),
blockLineCount: c.blockLineCount,
canvasOverdrag: c.overdrag * c.canvasPixelRatio
});
var groupWidth = Math.floor(width * (domain.x[1] - domain.x[0]));
var groupHeight = Math.floor(layout.height * (domain.y[1] - domain.y[0]));
var pad = layout.margin || {l: 80, r: 80, t: 100, b: 80};
var rowContentWidth = groupWidth;
var rowHeight = groupHeight;
return {
key: i,
colCount: dimensions.filter(visible).length,
dimensions: dimensions,
tickDistance: c.tickDistance,
unitToColor: unitToColorScale(cscale),
lines: lines,
translateX: domain.x[0] * width,
translateY: layout.height - domain.y[1] * layout.height,
pad: pad,
canvasWidth: rowContentWidth * c.canvasPixelRatio + 2 * lines.canvasOverdrag,
canvasHeight: rowHeight * c.canvasPixelRatio,
width: rowContentWidth,
height: rowHeight,
canvasPixelRatio: c.canvasPixelRatio
};
}
function viewModel(model) {
var width = model.width;
var height = model.height;
var dimensions = model.dimensions;
var canvasPixelRatio = model.canvasPixelRatio;
var xScale = function(d) {return width * d / Math.max(1, model.colCount - 1);};
var unitPad = c.verticalPadding / (height * canvasPixelRatio);
var unitPadScale = (1 - 2 * unitPad);
var paddedUnitScale = function(d) {return unitPad + unitPadScale * d;};
var viewModel = {
key: model.key,
xScale: xScale,
model: model
};
var uniqueKeys = {};
viewModel.dimensions = dimensions.filter(visible).map(function(dimension, i) {
var domainToUnit = domainToUnitScale(dimension);
var foundKey = uniqueKeys[dimension.label];
uniqueKeys[dimension.label] = (foundKey || 0) + 1;
var key = dimension.label + (foundKey ? '__' + foundKey : '');
return {
key: key,
label: dimension.label,
tickFormat: dimension.tickformat,
tickvals: dimension.tickvals,
ticktext: dimension.ticktext,
ordinal: !!dimension.tickvals,
scatter: c.scatter || dimension.scatter,
xIndex: i,
crossfilterDimensionIndex: i,
visibleIndex: dimension._index,
height: height,
values: dimension.values,
paddedUnitValues: dimension.values.map(domainToUnit).map(paddedUnitScale),
xScale: xScale,
x: xScale(i),
canvasX: xScale(i) * canvasPixelRatio,
unitScale: unitScale(height, c.verticalPadding),
domainScale: domainScale(height, c.verticalPadding, dimension),
ordinalScale: ordinalScale(dimension),
domainToUnitScale: domainToUnit,
filter: dimension.constraintrange ? dimension.constraintrange.map(domainToUnit) : [0, 1],
parent: viewModel,
model: model
};
});
return viewModel;
}
function lineLayerModel(vm) {
return c.layers.map(function(key) {
return {
key: key,
context: key === 'contextLineLayer',
pick: key === 'pickLineLayer',
viewModel: vm,
model: vm.model
};
});
}
function styleExtentTexts(selection) {
selection
.classed('axisExtentText', true)
.attr('text-anchor', 'middle')
.style('font-weight', 100)
.style('font-size', '10px')
.style('cursor', 'default')
.style('user-select', 'none');
}
module.exports = function(root, svg, styledData, layout, callbacks) {
var domainBrushing = false;
var linePickActive = true;
function enterSvgDefs(root) {
var defs = root.selectAll('defs')
.data(repeat, keyFun);
defs.enter()
.append('defs');
var filterBarPattern = defs.selectAll('#filterBarPattern')
.data(repeat, keyFun);
filterBarPattern.enter()
.append('pattern')
.attr('id', 'filterBarPattern')
.attr('patternUnits', 'userSpaceOnUse');
filterBarPattern
.attr('x', -c.bar.width)
.attr('width', c.bar.capturewidth)
.attr('height', function(d) {return d.model.height;});
var filterBarPatternGlyph = filterBarPattern.selectAll('rect')
.data(repeat, keyFun);
filterBarPatternGlyph.enter()
.append('rect')
.attr('shape-rendering', 'crispEdges');
filterBarPatternGlyph
.attr('height', function(d) {return d.model.height;})
.attr('width', c.bar.width)
.attr('x', c.bar.width / 2)
.attr('fill', c.bar.fillcolor)
.attr('fill-opacity', c.bar.fillopacity)
.attr('stroke', c.bar.strokecolor)
.attr('stroke-opacity', c.bar.strokeopacity)
.attr('stroke-width', c.bar.strokewidth);
}
var vm = styledData
.filter(function(d) { return unwrap(d).trace.visible; })
.map(model.bind(0, layout))
.map(viewModel);
root.selectAll('.parcoords-line-layers').remove();
var parcoordsLineLayers = root.selectAll('.parcoords-line-layers')
.data(vm, keyFun);
parcoordsLineLayers.enter()
.insert('div', '.' + svg.attr('class').split(' ').join(' .')) // not hardcoding .main-svg
.classed('parcoords-line-layers', true)
.style('box-sizing', 'content-box');
parcoordsLineLayers
.style('transform', function(d) {
return 'translate(' + (d.model.translateX - c.overdrag) + 'px,' + d.model.translateY + 'px)';
});
var parcoordsLineLayer = parcoordsLineLayers.selectAll('.parcoords-lines')
.data(lineLayerModel, keyFun);
var tweakables = {renderers: [], dimensions: []};
var lastHovered = null;
parcoordsLineLayer.enter()
.append('canvas')
.attr('class', function(d) {return 'parcoords-lines ' + (d.context ? 'context' : d.pick ? 'pick' : 'focus');})
.style('box-sizing', 'content-box')
.style('float', 'left')
.style('clear', 'both')
.style('left', 0)
.style('overflow', 'visible')
.style('position', function(d, i) {return i > 0 ? 'absolute' : 'absolute';})
.filter(function(d) {return d.pick;})
.on('mousemove', function(d) {
if(linePickActive && d.lineLayer && callbacks && callbacks.hover) {
var event = d3.event;
var cw = this.width;
var ch = this.height;
var pointer = d3.mouse(this);
var x = pointer[0];
var y = pointer[1];
if(x < 0 || y < 0 || x >= cw || y >= ch) {
return;
}
var pixel = d.lineLayer.readPixel(x, ch - 1 - y);
var found = pixel[3] !== 0;
// inverse of the calcPickColor in `lines.js`; detailed comment there
var curveNumber = found ? pixel[2] + 256 * (pixel[1] + 256 * pixel[0]) : null;
var eventData = {
x: x,
y: y,
clientX: event.clientX,
clientY: event.clientY,
dataIndex: d.model.key,
curveNumber: curveNumber
};
if(curveNumber !== lastHovered) { // don't unnecessarily repeat the same hit (or miss)
if(found) {
callbacks.hover(eventData);
} else if(callbacks.unhover) {
callbacks.unhover(eventData);
}
lastHovered = curveNumber;
}
}
});
parcoordsLineLayer
.style('margin', function(d) {
var p = d.model.pad;
return p.t + 'px ' + p.r + 'px ' + p.b + 'px ' + p.l + 'px';
})
.attr('width', function(d) {return d.model.canvasWidth;})
.attr('height', function(d) {return d.model.canvasHeight;})
.style('width', function(d) {return (d.model.width + 2 * c.overdrag) + 'px';})
.style('height', function(d) {return d.model.height + 'px';})
.style('opacity', function(d) {return d.pick ? 0.01 : 1;});
svg.style('background', 'rgba(255, 255, 255, 0)');
var parcoordsControlOverlay = svg.selectAll('.parcoords')
.data(vm, keyFun);
parcoordsControlOverlay.exit().remove();
parcoordsControlOverlay.enter()
.append('g')
.classed('parcoords', true)
.attr('overflow', 'visible')
.style('box-sizing', 'content-box')
.style('position', 'absolute')
.style('left', 0)
.style('overflow', 'visible')
.style('shape-rendering', 'crispEdges')
.style('pointer-events', 'none')
.call(enterSvgDefs);
parcoordsControlOverlay
.attr('width', function(d) {return d.model.width + d.model.pad.l + d.model.pad.r;})
.attr('height', function(d) {return d.model.height + d.model.pad.t + d.model.pad.b;})
.attr('transform', function(d) {
return 'translate(' + d.model.translateX + ',' + d.model.translateY + ')';
});
var parcoordsControlView = parcoordsControlOverlay.selectAll('.parcoordsControlView')
.data(repeat, keyFun);
parcoordsControlView.enter()
.append('g')
.classed('parcoordsControlView', true)
.style('box-sizing', 'content-box');
parcoordsControlView
.attr('transform', function(d) {return 'translate(' + d.model.pad.l + ',' + d.model.pad.t + ')';});
var yAxis = parcoordsControlView.selectAll('.yAxis')
.data(function(vm) {return vm.dimensions;}, keyFun);
function someFiltersActive(view) {
return view.dimensions.some(function(p) {return p.filter[0] !== 0 || p.filter[1] !== 1;});
}
function updatePanelLayoutParcoords(yAxis, vm) {
var panels = vm.panels || (vm.panels = []);
var yAxes = yAxis.each(function(d) {return d;})[vm.key].map(function(e) {return e.__data__;});
var panelCount = yAxes.length - 1;
var rowCount = 1;
for(var row = 0; row < rowCount; row++) {
for(var p = 0; p < panelCount; p++) {
var panel = panels[p + row * panelCount] || (panels[p + row * panelCount] = {});
var dim1 = yAxes[p];
var dim2 = yAxes[p + 1];
panel.dim1 = dim1;
panel.dim2 = dim2;
panel.canvasX = dim1.canvasX;
panel.panelSizeX = dim2.canvasX - dim1.canvasX;
panel.panelSizeY = vm.model.canvasHeight / rowCount;
panel.y = row * panel.panelSizeY;
panel.canvasY = vm.model.canvasHeight - panel.y - panel.panelSizeY;
}
}
}
function updatePanelLayoutScatter(yAxis, vm) {
var panels = vm.panels || (vm.panels = []);
var yAxes = yAxis.each(function(d) {return d;})[vm.key].map(function(e) {return e.__data__;});
var panelCount = yAxes.length - 1;
var rowCount = panelCount;
for(var row = 0; row < panelCount; row++) {
for(var p = 0; p < panelCount; p++) {
var panel = panels[p + row * panelCount] || (panels[p + row * panelCount] = {});
var dim1 = yAxes[p];
var dim2 = yAxes[p + 1];
panel.dim1 = yAxes[row + 1];
panel.dim2 = dim2;
panel.canvasX = dim1.canvasX;
panel.panelSizeX = dim2.canvasX - dim1.canvasX;
panel.panelSizeY = vm.model.canvasHeight / rowCount;
panel.y = row * panel.panelSizeY;
panel.canvasY = vm.model.canvasHeight - panel.y - panel.panelSizeY;
}
}
}
function updatePanelLayout(yAxis, vm) {
return (c.scatter ? updatePanelLayoutScatter : updatePanelLayoutParcoords)(yAxis, vm);
}
yAxis.enter()
.append('g')
.classed('yAxis', true)
.each(function(d) {tweakables.dimensions.push(d);});
parcoordsControlView.each(function(vm) {
updatePanelLayout(yAxis, vm);
});
parcoordsLineLayer
.each(function(d) {
d.lineLayer = lineLayerMaker(this, d.model.lines, d.model.canvasWidth, d.model.canvasHeight, d.viewModel.dimensions, d.viewModel.panels, d.model.unitToColor, d.context, d.pick, c.scatter);
d.viewModel[d.key] = d.lineLayer;
tweakables.renderers.push(function() {d.lineLayer.render(d.viewModel.panels, true);});
d.lineLayer.render(d.viewModel.panels, !d.context);
});
yAxis
.attr('transform', function(d) {return 'translate(' + d.xScale(d.xIndex) + ', 0)';});
yAxis
.call(d3.behavior.drag()
.origin(function(d) {return d;})
.on('drag', function(d) {
var p = d.parent;
linePickActive = false;
if(domainBrushing) {
return;
}
d.x = Math.max(-c.overdrag, Math.min(d.model.width + c.overdrag, d3.event.x));
d.canvasX = d.x * d.model.canvasPixelRatio;
yAxis
.sort(function(a, b) {return a.x - b.x;})
.each(function(dd, i) {
dd.xIndex = i;
dd.x = d === dd ? dd.x : dd.xScale(dd.xIndex);
dd.canvasX = dd.x * dd.model.canvasPixelRatio;
});
updatePanelLayout(yAxis, p);
yAxis.filter(function(dd) {return Math.abs(d.xIndex - dd.xIndex) !== 0;})
.attr('transform', function(d) {return 'translate(' + d.xScale(d.xIndex) + ', 0)';});
d3.select(this).attr('transform', 'translate(' + d.x + ', 0)');
yAxis.each(function(dd, i, ii) {if(ii === d.parent.key) p.dimensions[i] = dd;});
p.contextLineLayer && p.contextLineLayer.render(p.panels, false, !someFiltersActive(p));
p.focusLineLayer.render && p.focusLineLayer.render(p.panels);
})
.on('dragend', function(d) {
var p = d.parent;
if(domainBrushing) {
if(domainBrushing === 'ending') {
domainBrushing = false;
}
return;
}
d.x = d.xScale(d.xIndex);
d.canvasX = d.x * d.model.canvasPixelRatio;
updatePanelLayout(yAxis, p);
d3.select(this)
.attr('transform', function(d) {return 'translate(' + d.x + ', 0)';});
p.contextLineLayer && p.contextLineLayer.render(p.panels, false, !someFiltersActive(p));
p.focusLineLayer && p.focusLineLayer.render(p.panels);
p.pickLineLayer && p.pickLineLayer.render(p.panels, true);
linePickActive = true;
if(callbacks && callbacks.axesMoved) {
callbacks.axesMoved(p.key, p.dimensions.map(function(dd) {return dd.crossfilterDimensionIndex;}));
}
})
);
yAxis.exit()
.remove();
var axisOverlays = yAxis.selectAll('.axisOverlays')
.data(repeat, keyFun);
axisOverlays.enter()
.append('g')
.classed('axisOverlays', true);
axisOverlays.selectAll('.axis').remove();
var axis = axisOverlays.selectAll('.axis')
.data(repeat, keyFun);
axis.enter()
.append('g')
.classed('axis', true);
axis
.each(function(d) {
var wantedTickCount = d.model.height / d.model.tickDistance;
var scale = d.domainScale;
var sdom = scale.domain();
var texts = d.ticktext;
d3.select(this)
.call(d3.svg.axis()
.orient('left')
.tickSize(4)
.outerTickSize(2)
.ticks(wantedTickCount, d.tickFormat) // works for continuous scales only...
.tickValues(d.ordinal ? // and this works for ordinal scales
sdom.map(function(d, i) {return texts && texts[i] || d;}) :
null)
.tickFormat(d.ordinal ? function(d) {return d;} : null)
.scale(scale));
});
axis
.selectAll('.domain, .tick')
.attr('fill', 'none')
.attr('stroke', 'black')
.attr('stroke-opacity', 0.25)
.attr('stroke-width', '1px');
axis
.selectAll('text')
.style('font-weight', 100)
.style('font-size', '10px')
.style('fill', 'black')
.style('fill-opacity', 1)
.style('stroke', 'none')
.style('text-shadow', '1px 1px 1px #fff, -1px -1px 1px #fff, 1px -1px 1px #fff, -1px 1px 1px #fff')
.style('cursor', 'default')
.style('user-select', 'none');
var axisHeading = axisOverlays.selectAll('.axisHeading')
.data(repeat, keyFun);
axisHeading.enter()
.append('g')
.classed('axisHeading', true);
var axisTitle = axisHeading.selectAll('.axisTitle')
.data(repeat, keyFun);
axisTitle.enter()
.append('text')
.classed('axisTitle', true)
.attr('text-anchor', 'middle')
.style('font-family', 'sans-serif')
.style('font-size', '10px')
.style('cursor', 'ew-resize')
.style('user-select', 'none')
.style('pointer-events', 'auto');
axisTitle
.attr('transform', 'translate(0,' + -c.axisTitleOffset + ')')
.text(function(d) {return d.label;});
var axisExtent = axisOverlays.selectAll('.axisExtent')
.data(repeat, keyFun);
axisExtent.enter()
.append('g')
.classed('axisExtent', true);
var axisExtentTop = axisExtent.selectAll('.axisExtentTop')
.data(repeat, keyFun);
axisExtentTop.enter()
.append('g')
.classed('axisExtentTop', true);
axisExtentTop
.attr('transform', 'translate(' + 0 + ',' + -c.axisExtentOffset + ')');
var axisExtentTopText = axisExtentTop.selectAll('.axisExtentTopText')
.data(repeat, keyFun);
function formatExtreme(d) {
return d.ordinal ? function() {return '';} : d3.format(d.tickFormat);
}
axisExtentTopText.enter()
.append('text')
.classed('axisExtentTopText', true)
.attr('alignment-baseline', 'after-edge')
.call(styleExtentTexts);
axisExtentTopText
.text(function(d) {return formatExtreme(d)(d.domainScale.domain().slice(-1)[0]);});
var axisExtentBottom = axisExtent.selectAll('.axisExtentBottom')
.data(repeat, keyFun);
axisExtentBottom.enter()
.append('g')
.classed('axisExtentBottom', true);
axisExtentBottom
.attr('transform', function(d) {return 'translate(' + 0 + ',' + (d.model.height + c.axisExtentOffset) + ')';});
var axisExtentBottomText = axisExtentBottom.selectAll('.axisExtentBottomText')
.data(repeat, keyFun);
axisExtentBottomText.enter()
.append('text')
.classed('axisExtentBottomText', true)
.attr('alignment-baseline', 'before-edge')
.call(styleExtentTexts);
axisExtentBottomText
.text(function(d) {return formatExtreme(d)(d.domainScale.domain()[0]);});
var axisBrush = axisOverlays.selectAll('.axisBrush')
.data(repeat, keyFun);
var axisBrushEnter = axisBrush.enter()
.append('g')
.classed('axisBrush', true);
axisBrush
.each(function(d) {
if(!d.brush) {
d.brush = d3.svg.brush()
.y(d.unitScale)
.on('brushstart', axisBrushStarted)
.on('brush', axisBrushMoved)
.on('brushend', axisBrushEnded);
if(d.filter[0] !== 0 || d.filter[1] !== 1) {
d.brush.extent(d.filter);
}
d3.select(this).call(d.brush);
}
});
axisBrushEnter
.selectAll('rect')
.attr('x', -c.bar.capturewidth / 2)
.attr('width', c.bar.capturewidth);
axisBrushEnter
.selectAll('rect.extent')
.attr('fill', 'url(#filterBarPattern)')
.style('cursor', 'ns-resize')
.filter(function(d) {return d.filter[0] === 0 && d.filter[1] === 1;})
.attr('y', -100); // // zero-size rectangle pointer issue workaround
axisBrushEnter
.selectAll('.resize rect')
.attr('height', c.bar.handleheight)
.attr('opacity', 0)
.style('visibility', 'visible');
axisBrushEnter
.selectAll('.resize.n rect')
.style('cursor', 'n-resize')
.attr('y', c.bar.handleoverlap - c.bar.handleheight);
axisBrushEnter
.selectAll('.resize.s rect')
.style('cursor', 's-resize')
.attr('y', c.bar.handleoverlap);
var justStarted = false;
var contextShown = false;
function axisBrushStarted() {
justStarted = true;
domainBrushing = true;
}
function axisBrushMoved(dimension) {
linePickActive = false;
var p = dimension.parent;
var extent = dimension.brush.extent();
var dimensions = p.dimensions;
var filter = dimensions[dimension.xIndex].filter;
var reset = justStarted && (extent[0] === extent[1]);
if(reset) {
dimension.brush.clear();
d3.select(this).select('rect.extent').attr('y', -100); // zero-size rectangle pointer issue workaround
}
var newExtent = reset ? [0, 1] : extent.slice();
if(newExtent[0] !== filter[0] || newExtent[1] !== filter[1]) {
dimensions[dimension.xIndex].filter = newExtent;
p.focusLineLayer && p.focusLineLayer.render(p.panels, true);
var filtersActive = someFiltersActive(p);
if(!contextShown && filtersActive) {
p.contextLineLayer && p.contextLineLayer.render(p.panels, true);
contextShown = true;
} else if(contextShown && !filtersActive) {
p.contextLineLayer && p.contextLineLayer.render(p.panels, true, true);
contextShown = false;
}
}
justStarted = false;
}
function axisBrushEnded(dimension) {
var p = dimension.parent;
var extent = dimension.brush.extent();
var empty = extent[0] === extent[1];
var dimensions = p.dimensions;
var f = dimensions[dimension.xIndex].filter;
if(!empty && dimension.ordinal) {
f[0] = ordinalScaleSnap(dimension.ordinalScale, f[0]);
f[1] = ordinalScaleSnap(dimension.ordinalScale, f[1]);
if(f[0] === f[1]) {
f[0] = Math.max(0, f[0] - 0.05);
f[1] = Math.min(1, f[1] + 0.05);
}
d3.select(this).transition().duration(150).call(dimension.brush.extent(f));
p.focusLineLayer.render(p.panels, true);
}
p.pickLineLayer && p.pickLineLayer.render(p.panels, true);
linePickActive = true;
domainBrushing = 'ending';
if(callbacks && callbacks.filterChanged) {
var invScale = dimension.domainToUnitScale.invert;
// update gd.data as if a Plotly.restyle were fired
var newRange = f.map(invScale);
callbacks.filterChanged(p.key, dimension.visibleIndex, newRange);
}
}
return tweakables;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var parcoords = require('./parcoords');
module.exports = function plot(gd, cdparcoords) {
var fullLayout = gd._fullLayout;
var svg = fullLayout._paper;
var root = fullLayout._paperdiv;
var gdDimensions = {};
var gdDimensionsOriginalOrder = {};
var size = fullLayout._size;
cdparcoords.forEach(function(d, i) {
gdDimensions[i] = gd.data[i].dimensions;
gdDimensionsOriginalOrder[i] = gd.data[i].dimensions.slice();
});
var filterChanged = function(i, originalDimensionIndex, newRange) {
// Have updated `constraintrange` data on `gd.data` and raise `Plotly.restyle` event
// without having to incur heavy UI blocking due to an actual `Plotly.restyle` call
var gdDimension = gdDimensionsOriginalOrder[i][originalDimensionIndex];
var gdConstraintRange = gdDimension.constraintrange;
if(!gdConstraintRange || gdConstraintRange.length !== 2) {
gdConstraintRange = gdDimension.constraintrange = [];
}
gdConstraintRange[0] = newRange[0];
gdConstraintRange[1] = newRange[1];
gd.emit('plotly_restyle');
};
var hover = function(eventData) {
gd.emit('plotly_hover', eventData);
};
var unhover = function(eventData) {
gd.emit('plotly_unhover', eventData);
};
var axesMoved = function(i, visibleIndices) {
// Have updated order data on `gd.data` and raise `Plotly.restyle` event
// without having to incur heavy UI blocking due to an actual `Plotly.restyle` call
function visible(dimension) {return !('visible' in dimension) || dimension.visible;}
function newIdx(visibleIndices, orig, dim) {
var origIndex = orig.indexOf(dim);
var currentIndex = visibleIndices.indexOf(origIndex);
if(currentIndex === -1) {
// invisible dimensions initially go to the end
currentIndex += orig.length;
}
return currentIndex;
}
function sorter(orig) {
return function sorter(d1, d2) {
var i1 = newIdx(visibleIndices, orig, d1);
var i2 = newIdx(visibleIndices, orig, d2);
return i1 - i2;
};
}
// drag&drop sorting of the visible dimensions
var orig = sorter(gdDimensionsOriginalOrder[i].filter(visible));
gdDimensions[i].sort(orig);
// invisible dimensions are not interpreted in the context of drag&drop sorting as an invisible dimension
// cannot be dragged; they're interspersed into their original positions by this subsequent merging step
gdDimensionsOriginalOrder[i].filter(function(d) {return !visible(d);})
.sort(function(d) {
// subsequent splicing to be done left to right, otherwise indices may be incorrect
return gdDimensionsOriginalOrder[i].indexOf(d);
})
.forEach(function(d) {
gdDimensions[i].splice(gdDimensions[i].indexOf(d), 1); // remove from the end
gdDimensions[i].splice(gdDimensionsOriginalOrder[i].indexOf(d), 0, d); // insert at original index
});
gd.emit('plotly_restyle');
};
parcoords(
root,
svg,
cdparcoords,
{
width: size.w,
height: size.h,
margin: {
t: size.t,
r: size.r,
b: size.b,
l: size.l
}
},
{
filterChanged: filterChanged,
hover: hover,
unhover: unhover,
axesMoved: axesMoved
});
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (5 / 5) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (5 / 5) | |
| base_plot.js | 25% | (5 / 20) | 0% | (0 / 14) | 0% | (0 / 3) | 26.32% | (5 / 19) | |
| calc.js | 9.72% | (7 / 72) | 0% | (0 / 46) | 0% | (0 / 3) | 11.67% | (7 / 60) | |
| defaults.js | 10% | (4 / 40) | 0% | (0 / 32) | 0% | (0 / 2) | 11.11% | (4 / 36) | |
| helpers.js | 27.27% | (3 / 11) | 0% | (0 / 4) | 0% | (0 / 2) | 27.27% | (3 / 11) | |
| index.js | 100% | (15 / 15) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (15 / 15) | |
| layout_attributes.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| layout_defaults.js | 66.67% | (4 / 6) | 100% | (0 / 0) | 0% | (0 / 2) | 66.67% | (4 / 6) | |
| plot.js | 7.83% | (22 / 281) | 0% | (0 / 196) | 0% | (0 / 24) | 8.66% | (22 / 254) | |
| style.js | 37.5% | (3 / 8) | 100% | (0 / 0) | 0% | (0 / 3) | 37.5% | (3 / 8) | |
| style_one.js | 22.22% | (2 / 9) | 0% | (0 / 10) | 0% | (0 / 1) | 28.57% | (2 / 7) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorAttrs = require('../../components/color/attributes');
var fontAttrs = require('../../plots/font_attributes');
var plotAttrs = require('../../plots/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
labels: {
valType: 'data_array',
description: 'Sets the sector labels.'
},
// equivalent of x0 and dx, if label is missing
label0: {
valType: 'number',
role: 'info',
dflt: 0,
description: [
'Alternate to `labels`.',
'Builds a numeric set of labels.',
'Use with `dlabel`',
'where `label0` is the starting label and `dlabel` the step.'
].join(' ')
},
dlabel: {
valType: 'number',
role: 'info',
dflt: 1,
description: 'Sets the label step. See `label0` for more info.'
},
values: {
valType: 'data_array',
description: 'Sets the values of the sectors of this pie chart.'
},
marker: {
colors: {
valType: 'data_array', // TODO 'color_array' ?
description: [
'Sets the color of each sector of this pie chart.',
'If not specified, the default trace color set is used',
'to pick the sector colors.'
].join(' ')
},
line: {
color: {
valType: 'color',
role: 'style',
dflt: colorAttrs.defaultLine,
arrayOk: true,
description: [
'Sets the color of the line enclosing each sector.'
].join(' ')
},
width: {
valType: 'number',
role: 'style',
min: 0,
dflt: 0,
arrayOk: true,
description: [
'Sets the width (in px) of the line enclosing each sector.'
].join(' ')
}
}
},
text: {
valType: 'data_array',
description: [
'Sets text elements associated with each sector.',
'If trace `textinfo` contains a *text* flag, these elements will seen',
'on the chart.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertext: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets hover text elements associated with each sector.',
'If a single string, the same string appears for',
'all data points.',
'If an array of string, the items are mapped in order of',
'this trace\'s sectors.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
},
// 'see eg:'
// 'https://www.e-education.psu.edu/natureofgeoinfo/sites/www.e-education.psu.edu.natureofgeoinfo/files/image/hisp_pies.gif',
// '(this example involves a map too - may someday be a whole trace type',
// 'of its own. but the point is the size of the whole pie is important.)'
scalegroup: {
valType: 'string',
role: 'info',
dflt: '',
description: [
'If there are multiple pies that should be sized according to',
'their totals, link them by providing a non-empty group id here',
'shared by every trace in the same group.'
].join(' ')
},
// labels (legend is handled by plots.attributes.showlegend and layout.hiddenlabels)
textinfo: {
valType: 'flaglist',
role: 'info',
flags: ['label', 'text', 'value', 'percent'],
extras: ['none'],
description: [
'Determines which trace information appear on the graph.'
].join(' ')
},
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['label', 'text', 'value', 'percent', 'name']
}),
textposition: {
valType: 'enumerated',
role: 'info',
values: ['inside', 'outside', 'auto', 'none'],
dflt: 'auto',
arrayOk: true,
description: [
'Specifies the location of the `textinfo`.'
].join(' ')
},
// TODO make those arrayOk?
textfont: extendFlat({}, fontAttrs, {
description: 'Sets the font used for `textinfo`.'
}),
insidetextfont: extendFlat({}, fontAttrs, {
description: 'Sets the font used for `textinfo` lying inside the pie.'
}),
outsidetextfont: extendFlat({}, fontAttrs, {
description: 'Sets the font used for `textinfo` lying outside the pie.'
}),
// position and shape
domain: {
x: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1},
{valType: 'number', min: 0, max: 1}
],
dflt: [0, 1],
description: [
'Sets the horizontal domain of this pie trace',
'(in plot fraction).'
].join(' ')
},
y: {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1},
{valType: 'number', min: 0, max: 1}
],
dflt: [0, 1],
description: [
'Sets the vertical domain of this pie trace',
'(in plot fraction).'
].join(' ')
}
},
hole: {
valType: 'number',
role: 'style',
min: 0,
max: 1,
dflt: 0,
description: [
'Sets the fraction of the radius to cut out of the pie.',
'Use this to make a donut chart.'
].join(' ')
},
// ordering and direction
sort: {
valType: 'boolean',
role: 'style',
dflt: true,
description: [
'Determines whether or not the sectors are reordered',
'from largest to smallest.'
].join(' ')
},
direction: {
/**
* there are two common conventions, both of which place the first
* (largest, if sorted) slice with its left edge at 12 o'clock but
* succeeding slices follow either cw or ccw from there.
*
* see http://visage.co/data-visualization-101-pie-charts/
*/
valType: 'enumerated',
values: ['clockwise', 'counterclockwise'],
role: 'style',
dflt: 'counterclockwise',
description: [
'Specifies the direction at which succeeding sectors follow',
'one another.'
].join(' ')
},
rotation: {
valType: 'number',
role: 'style',
min: -360,
max: 360,
dflt: 0,
description: [
'Instead of the first slice starting at 12 o\'clock,',
'rotate to some other angle.'
].join(' ')
},
pull: {
valType: 'number',
role: 'style',
min: 0,
max: 1,
dflt: 0,
arrayOk: true,
description: [
'Sets the fraction of larger radius to pull the sectors',
'out from the center. This can be a constant',
'to pull all slices apart from each other equally',
'or an array to highlight one or more slices.'
].join(' ')
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
exports.name = 'pie';
exports.plot = function(gd) {
var Pie = Registry.getModule('pie');
var cdPie = getCdModule(gd.calcdata, Pie);
if(cdPie.length) Pie.plot(gd, cdPie);
};
exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout) {
var hadPie = (oldFullLayout._has && oldFullLayout._has('pie'));
var hasPie = (newFullLayout._has && newFullLayout._has('pie'));
if(hadPie && !hasPie) {
oldFullLayout._pielayer.selectAll('g.trace').remove();
}
};
function getCdModule(calcdata, _module) {
var cdModule = [];
for(var i = 0; i < calcdata.length; i++) {
var cd = calcdata[i];
var trace = cd[0].trace;
if((trace._module === _module) && (trace.visible === true)) {
cdModule.push(cd);
}
}
return cdModule;
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var tinycolor = require('tinycolor2');
var Color = require('../../components/color');
var helpers = require('./helpers');
module.exports = function calc(gd, trace) {
var vals = trace.values,
labels = trace.labels,
cd = [],
fullLayout = gd._fullLayout,
colorMap = fullLayout._piecolormap,
allThisTraceLabels = {},
needDefaults = false,
vTotal = 0,
hiddenLabels = fullLayout.hiddenlabels || [],
i,
v,
label,
color,
hidden,
pt;
if(trace.dlabel) {
labels = new Array(vals.length);
for(i = 0; i < vals.length; i++) {
labels[i] = String(trace.label0 + i * trace.dlabel);
}
}
for(i = 0; i < vals.length; i++) {
v = vals[i];
if(!isNumeric(v)) continue;
v = +v;
if(v < 0) continue;
label = labels[i];
if(label === undefined || label === '') label = i;
label = String(label);
// only take the first occurrence of any given label.
// TODO: perhaps (optionally?) sum values for a repeated label?
if(allThisTraceLabels[label] === undefined) allThisTraceLabels[label] = true;
else continue;
color = tinycolor(trace.marker.colors[i]);
if(color.isValid()) {
color = Color.addOpacity(color, color.getAlpha());
if(!colorMap[label]) {
colorMap[label] = color;
}
}
// have we seen this label and assigned a color to it in a previous trace?
else if(colorMap[label]) color = colorMap[label];
// color needs a default - mark it false, come back after sorting
else {
color = false;
needDefaults = true;
}
hidden = hiddenLabels.indexOf(label) !== -1;
if(!hidden) vTotal += v;
cd.push({
v: v,
label: label,
color: color,
i: i,
hidden: hidden
});
}
if(trace.sort) cd.sort(function(a, b) { return b.v - a.v; });
/**
* now go back and fill in colors we're still missing
* this is done after sorting, so we pick defaults
* in the order slices will be displayed
*/
if(needDefaults) {
for(i = 0; i < cd.length; i++) {
pt = cd[i];
if(pt.color === false) {
colorMap[pt.label] = pt.color = nextDefaultColor(fullLayout._piedefaultcolorcount);
fullLayout._piedefaultcolorcount++;
}
}
}
// include the sum of all values in the first point
if(cd[0]) cd[0].vTotal = vTotal;
// now insert text
if(trace.textinfo && trace.textinfo !== 'none') {
var hasLabel = trace.textinfo.indexOf('label') !== -1,
hasText = trace.textinfo.indexOf('text') !== -1,
hasValue = trace.textinfo.indexOf('value') !== -1,
hasPercent = trace.textinfo.indexOf('percent') !== -1,
separators = fullLayout.separators,
thisText;
for(i = 0; i < cd.length; i++) {
pt = cd[i];
thisText = hasLabel ? [pt.label] : [];
if(hasText && trace.text[pt.i]) thisText.push(trace.text[pt.i]);
if(hasValue) thisText.push(helpers.formatPieValue(pt.v, separators));
if(hasPercent) thisText.push(helpers.formatPiePercent(pt.v / vTotal, separators));
pt.text = thisText.join('<br>');
}
}
return cd;
};
/**
* pick a default color from the main default set, augmented by
* itself lighter then darker before repeating
*/
var pieDefaultColors;
function nextDefaultColor(index) {
if(!pieDefaultColors) {
// generate this default set on demand (but then it gets saved in the module)
var mainDefaults = Color.defaults;
pieDefaultColors = mainDefaults.slice();
var i;
for(i = 0; i < mainDefaults.length; i++) {
pieDefaultColors.push(tinycolor(mainDefaults[i]).lighten(20).toHexString());
}
for(i = 0; i < Color.defaults.length; i++) {
pieDefaultColors.push(tinycolor(mainDefaults[i]).darken(20).toHexString());
}
}
return pieDefaultColors[index % pieDefaultColors.length];
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var attributes = require('./attributes');
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}
var coerceFont = Lib.coerceFont;
var vals = coerce('values');
if(!Array.isArray(vals) || !vals.length) {
traceOut.visible = false;
return;
}
var labels = coerce('labels');
if(!Array.isArray(labels)) {
coerce('label0');
coerce('dlabel');
}
var lineWidth = coerce('marker.line.width');
if(lineWidth) coerce('marker.line.color');
var colors = coerce('marker.colors');
if(!Array.isArray(colors)) traceOut.marker.colors = []; // later this will get padded with default colors
coerce('scalegroup');
// TODO: tilt, depth, and hole all need to be coerced to the same values within a scaleegroup
// (ideally actually, depth would get set the same *after* scaling, ie the same absolute depth)
// and if colors aren't specified we should match these up - potentially even if separate pies
// are NOT in the same sharegroup
var textData = coerce('text');
var textInfo = coerce('textinfo', Array.isArray(textData) ? 'text+percent' : 'percent');
coerce('hovertext');
coerce('hoverinfo', (layout._dataLength === 1) ? 'label+text+value+percent' : undefined);
if(textInfo && textInfo !== 'none') {
var textPosition = coerce('textposition'),
hasBoth = Array.isArray(textPosition) || textPosition === 'auto',
hasInside = hasBoth || textPosition === 'inside',
hasOutside = hasBoth || textPosition === 'outside';
if(hasInside || hasOutside) {
var dfltFont = coerceFont(coerce, 'textfont', layout.font);
if(hasInside) coerceFont(coerce, 'insidetextfont', dfltFont);
if(hasOutside) coerceFont(coerce, 'outsidetextfont', dfltFont);
}
}
coerce('domain.x');
coerce('domain.y');
// 3D attributes commented out until I finish them in a later PR
// var tilt = coerce('tilt');
// if(tilt) {
// coerce('tiltaxis');
// coerce('depth');
// coerce('shading');
// }
coerce('hole');
coerce('sort');
coerce('direction');
coerce('rotation');
coerce('pull');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
exports.formatPiePercent = function formatPiePercent(v, separators) {
var vRounded = (v * 100).toPrecision(3);
if(vRounded.lastIndexOf('.') !== -1) {
vRounded = vRounded.replace(/[.]?0+$/, '');
}
return Lib.numSeparate(vRounded, separators) + '%';
};
exports.formatPieValue = function formatPieValue(v, separators) {
var vRounded = v.toPrecision(10);
if(vRounded.lastIndexOf('.') !== -1) {
vRounded = vRounded.replace(/[.]?0+$/, '');
}
return Lib.numSeparate(vRounded, separators);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Pie = {};
Pie.attributes = require('./attributes');
Pie.supplyDefaults = require('./defaults');
Pie.supplyLayoutDefaults = require('./layout_defaults');
Pie.layoutAttributes = require('./layout_attributes');
Pie.calc = require('./calc');
Pie.plot = require('./plot');
Pie.style = require('./style');
Pie.styleOne = require('./style_one');
Pie.moduleType = 'trace';
Pie.name = 'pie';
Pie.basePlotModule = require('./base_plot');
Pie.categories = ['pie', 'showLegend'];
Pie.meta = {
description: [
'A data visualized by the sectors of the pie is set in `values`.',
'The sector labels are set in `labels`.',
'The sector colors are set in `marker.colors`'
].join(' ')
};
module.exports = Pie;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
/**
* hiddenlabels is the pie chart analog of visible:'legendonly'
* but it can contain many labels, and can hide slices
* from several pies simultaneously
*/
hiddenlabels: {valType: 'data_array'}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var layoutAttributes = require('./layout_attributes');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {
function coerce(attr, dflt) {
return Lib.coerce(layoutIn, layoutOut, layoutAttributes, attr, dflt);
}
coerce('hiddenlabels');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var Fx = require('../../plots/cartesian/graph_interact');
var Color = require('../../components/color');
var Drawing = require('../../components/drawing');
var svgTextUtils = require('../../lib/svg_text_utils');
var helpers = require('./helpers');
module.exports = function plot(gd, cdpie) {
var fullLayout = gd._fullLayout;
scalePies(cdpie, fullLayout._size);
var pieGroups = fullLayout._pielayer.selectAll('g.trace').data(cdpie);
pieGroups.enter().append('g')
.attr({
'stroke-linejoin': 'round', // TODO: miter might look better but can sometimes cause problems
// maybe miter with a small-ish stroke-miterlimit?
'class': 'trace'
});
pieGroups.exit().remove();
pieGroups.order();
pieGroups.each(function(cd) {
var pieGroup = d3.select(this),
cd0 = cd[0],
trace = cd0.trace,
tiltRads = 0, // trace.tilt * Math.PI / 180,
depthLength = (trace.depth||0) * cd0.r * Math.sin(tiltRads) / 2,
tiltAxis = trace.tiltaxis || 0,
tiltAxisRads = tiltAxis * Math.PI / 180,
depthVector = [
depthLength * Math.sin(tiltAxisRads),
depthLength * Math.cos(tiltAxisRads)
],
rSmall = cd0.r * Math.cos(tiltRads);
var pieParts = pieGroup.selectAll('g.part')
.data(trace.tilt ? ['top', 'sides'] : ['top']);
pieParts.enter().append('g').attr('class', function(d) {
return d + ' part';
});
pieParts.exit().remove();
pieParts.order();
setCoords(cd);
pieGroup.selectAll('.top').each(function() {
var slices = d3.select(this).selectAll('g.slice').data(cd);
slices.enter().append('g')
.classed('slice', true);
slices.exit().remove();
var quadrants = [
[[], []], // y<0: x<0, x>=0
[[], []] // y>=0: x<0, x>=0
],
hasOutsideText = false;
slices.each(function(pt) {
if(pt.hidden) {
d3.select(this).selectAll('path,g').remove();
return;
}
quadrants[pt.pxmid[1] < 0 ? 0 : 1][pt.pxmid[0] < 0 ? 0 : 1].push(pt);
var cx = cd0.cx + depthVector[0],
cy = cd0.cy + depthVector[1],
sliceTop = d3.select(this),
slicePath = sliceTop.selectAll('path.surface').data([pt]),
hasHoverData = false;
function handleMouseOver(evt) {
evt.originalEvent = d3.event;
// in case fullLayout or fullData has changed without a replot
var fullLayout2 = gd._fullLayout,
trace2 = gd._fullData[trace.index],
hoverinfo = trace2.hoverinfo;
if(hoverinfo === 'all') hoverinfo = 'label+text+value+percent+name';
// in case we dragged over the pie from another subplot,
// or if hover is turned off
if(gd._dragging || fullLayout2.hovermode === false ||
hoverinfo === 'none' || hoverinfo === 'skip' || !hoverinfo) {
Fx.hover(gd, evt, 'pie');
return;
}
var rInscribed = getInscribedRadiusFraction(pt, cd0),
hoverCenterX = cx + pt.pxmid[0] * (1 - rInscribed),
hoverCenterY = cy + pt.pxmid[1] * (1 - rInscribed),
separators = fullLayout.separators,
thisText = [];
if(hoverinfo.indexOf('label') !== -1) thisText.push(pt.label);
if(hoverinfo.indexOf('text') !== -1) {
if(trace2.hovertext) {
thisText.push(
Array.isArray(trace2.hovertext) ?
trace2.hovertext[pt.i] :
trace2.hovertext
);
} else if(trace2.text && trace2.text[pt.i]) {
thisText.push(trace2.text[pt.i]);
}
}
if(hoverinfo.indexOf('value') !== -1) thisText.push(helpers.formatPieValue(pt.v, separators));
if(hoverinfo.indexOf('percent') !== -1) thisText.push(helpers.formatPiePercent(pt.v / cd0.vTotal, separators));
Fx.loneHover({
x0: hoverCenterX - rInscribed * cd0.r,
x1: hoverCenterX + rInscribed * cd0.r,
y: hoverCenterY,
text: thisText.join('<br>'),
name: hoverinfo.indexOf('name') !== -1 ? trace2.name : undefined,
color: pt.color,
idealAlign: pt.pxmid[0] < 0 ? 'left' : 'right'
}, {
container: fullLayout2._hoverlayer.node(),
outerContainer: fullLayout2._paper.node()
});
Fx.hover(gd, evt, 'pie');
hasHoverData = true;
}
function handleMouseOut(evt) {
evt.originalEvent = d3.event;
gd.emit('plotly_unhover', {
event: d3.event,
points: [evt]
});
if(hasHoverData) {
Fx.loneUnhover(fullLayout._hoverlayer.node());
hasHoverData = false;
}
}
function handleClick() {
gd._hoverdata = [pt];
gd._hoverdata.trace = cd0.trace;
Fx.click(gd, d3.event);
}
slicePath.enter().append('path')
.classed('surface', true)
.style({'pointer-events': 'all'});
sliceTop.select('path.textline').remove();
sliceTop
.on('mouseover', handleMouseOver)
.on('mouseout', handleMouseOut)
.on('click', handleClick);
if(trace.pull) {
var pull = +(Array.isArray(trace.pull) ? trace.pull[pt.i] : trace.pull) || 0;
if(pull > 0) {
cx += pull * pt.pxmid[0];
cy += pull * pt.pxmid[1];
}
}
pt.cxFinal = cx;
pt.cyFinal = cy;
function arc(start, finish, cw, scale) {
return 'a' + (scale * cd0.r) + ',' + (scale * rSmall) + ' ' + tiltAxis + ' ' +
pt.largeArc + (cw ? ' 1 ' : ' 0 ') +
(scale * (finish[0] - start[0])) + ',' + (scale * (finish[1] - start[1]));
}
var hole = trace.hole;
if(pt.v === cd0.vTotal) { // 100% fails bcs arc start and end are identical
var outerCircle = 'M' + (cx + pt.px0[0]) + ',' + (cy + pt.px0[1]) +
arc(pt.px0, pt.pxmid, true, 1) +
arc(pt.pxmid, pt.px0, true, 1) + 'Z';
if(hole) {
slicePath.attr('d',
'M' + (cx + hole * pt.px0[0]) + ',' + (cy + hole * pt.px0[1]) +
arc(pt.px0, pt.pxmid, false, hole) +
arc(pt.pxmid, pt.px0, false, hole) +
'Z' + outerCircle);
}
else slicePath.attr('d', outerCircle);
} else {
var outerArc = arc(pt.px0, pt.px1, true, 1);
if(hole) {
var rim = 1 - hole;
slicePath.attr('d',
'M' + (cx + hole * pt.px1[0]) + ',' + (cy + hole * pt.px1[1]) +
arc(pt.px1, pt.px0, false, hole) +
'l' + (rim * pt.px0[0]) + ',' + (rim * pt.px0[1]) +
outerArc +
'Z');
} else {
slicePath.attr('d',
'M' + cx + ',' + cy +
'l' + pt.px0[0] + ',' + pt.px0[1] +
outerArc +
'Z');
}
}
// add text
var textPosition = Array.isArray(trace.textposition) ?
trace.textposition[pt.i] : trace.textposition,
sliceTextGroup = sliceTop.selectAll('g.slicetext')
.data(pt.text && (textPosition !== 'none') ? [0] : []);
sliceTextGroup.enter().append('g')
.classed('slicetext', true);
sliceTextGroup.exit().remove();
sliceTextGroup.each(function() {
var sliceText = d3.select(this).selectAll('text').data([0]);
sliceText.enter().append('text')
// prohibit tex interpretation until we can handle
// tex and regular text together
.attr('data-notex', 1);
sliceText.exit().remove();
sliceText.text(pt.text)
.attr({
'class': 'slicetext',
transform: '',
'data-bb': '',
'text-anchor': 'middle',
x: 0,
y: 0
})
.call(Drawing.font, textPosition === 'outside' ?
trace.outsidetextfont : trace.insidetextfont)
.call(svgTextUtils.convertToTspans);
sliceText.selectAll('tspan.line').attr({x: 0, y: 0});
// position the text relative to the slice
// TODO: so far this only accounts for flat
var textBB = Drawing.bBox(sliceText.node()),
transform;
if(textPosition === 'outside') {
transform = transformOutsideText(textBB, pt);
} else {
transform = transformInsideText(textBB, pt, cd0);
if(textPosition === 'auto' && transform.scale < 1) {
sliceText.call(Drawing.font, trace.outsidetextfont);
if(trace.outsidetextfont.family !== trace.insidetextfont.family ||
trace.outsidetextfont.size !== trace.insidetextfont.size) {
sliceText.attr({'data-bb': ''});
textBB = Drawing.bBox(sliceText.node());
}
transform = transformOutsideText(textBB, pt);
}
}
var translateX = cx + pt.pxmid[0] * transform.rCenter + (transform.x || 0),
translateY = cy + pt.pxmid[1] * transform.rCenter + (transform.y || 0);
// save some stuff to use later ensure no labels overlap
if(transform.outside) {
pt.yLabelMin = translateY - textBB.height / 2;
pt.yLabelMid = translateY;
pt.yLabelMax = translateY + textBB.height / 2;
pt.labelExtraX = 0;
pt.labelExtraY = 0;
hasOutsideText = true;
}
sliceText.attr('transform',
'translate(' + translateX + ',' + translateY + ')' +
(transform.scale < 1 ? ('scale(' + transform.scale + ')') : '') +
(transform.rotate ? ('rotate(' + transform.rotate + ')') : '') +
'translate(' +
(-(textBB.left + textBB.right) / 2) + ',' +
(-(textBB.top + textBB.bottom) / 2) +
')');
});
});
// now make sure no labels overlap (at least within one pie)
if(hasOutsideText) scootLabels(quadrants, trace);
slices.each(function(pt) {
if(pt.labelExtraX || pt.labelExtraY) {
// first move the text to its new location
var sliceTop = d3.select(this),
sliceText = sliceTop.select('g.slicetext text');
sliceText.attr('transform', 'translate(' + pt.labelExtraX + ',' + pt.labelExtraY + ')' +
sliceText.attr('transform'));
// then add a line to the new location
var lineStartX = pt.cxFinal + pt.pxmid[0],
lineStartY = pt.cyFinal + pt.pxmid[1],
textLinePath = 'M' + lineStartX + ',' + lineStartY,
finalX = (pt.yLabelMax - pt.yLabelMin) * (pt.pxmid[0] < 0 ? -1 : 1) / 4;
if(pt.labelExtraX) {
var yFromX = pt.labelExtraX * pt.pxmid[1] / pt.pxmid[0],
yNet = pt.yLabelMid + pt.labelExtraY - (pt.cyFinal + pt.pxmid[1]);
if(Math.abs(yFromX) > Math.abs(yNet)) {
textLinePath +=
'l' + (yNet * pt.pxmid[0] / pt.pxmid[1]) + ',' + yNet +
'H' + (lineStartX + pt.labelExtraX + finalX);
} else {
textLinePath += 'l' + pt.labelExtraX + ',' + yFromX +
'v' + (yNet - yFromX) +
'h' + finalX;
}
} else {
textLinePath +=
'V' + (pt.yLabelMid + pt.labelExtraY) +
'h' + finalX;
}
sliceTop.append('path')
.classed('textline', true)
.call(Color.stroke, trace.outsidetextfont.color)
.attr({
'stroke-width': Math.min(2, trace.outsidetextfont.size / 8),
d: textLinePath,
fill: 'none'
});
}
});
});
});
// This is for a bug in Chrome (as of 2015-07-22, and does not affect FF)
// if insidetextfont and outsidetextfont are different sizes, sometimes the size
// of an "em" gets taken from the wrong element at first so lines are
// spaced wrong. You just have to tell it to try again later and it gets fixed.
// I have no idea why we haven't seen this in other contexts. Also, sometimes
// it gets the initial draw correct but on redraw it gets confused.
setTimeout(function() {
pieGroups.selectAll('tspan').each(function() {
var s = d3.select(this);
if(s.attr('dy')) s.attr('dy', s.attr('dy'));
});
}, 0);
};
function transformInsideText(textBB, pt, cd0) {
var textDiameter = Math.sqrt(textBB.width * textBB.width + textBB.height * textBB.height),
textAspect = textBB.width / textBB.height,
halfAngle = Math.PI * Math.min(pt.v / cd0.vTotal, 0.5),
ring = 1 - cd0.trace.hole,
rInscribed = getInscribedRadiusFraction(pt, cd0),
// max size text can be inserted inside without rotating it
// this inscribes the text rectangle in a circle, which is then inscribed
// in the slice, so it will be an underestimate, which some day we may want
// to improve so this case can get more use
transform = {
scale: rInscribed * cd0.r * 2 / textDiameter,
// and the center position and rotation in this case
rCenter: 1 - rInscribed,
rotate: 0
};
if(transform.scale >= 1) return transform;
// max size if text is rotated radially
var Qr = textAspect + 1 / (2 * Math.tan(halfAngle)),
maxHalfHeightRotRadial = cd0.r * Math.min(
1 / (Math.sqrt(Qr * Qr + 0.5) + Qr),
ring / (Math.sqrt(textAspect * textAspect + ring / 2) + textAspect)
),
radialTransform = {
scale: maxHalfHeightRotRadial * 2 / textBB.height,
rCenter: Math.cos(maxHalfHeightRotRadial / cd0.r) -
maxHalfHeightRotRadial * textAspect / cd0.r,
rotate: (180 / Math.PI * pt.midangle + 720) % 180 - 90
},
// max size if text is rotated tangentially
aspectInv = 1 / textAspect,
Qt = aspectInv + 1 / (2 * Math.tan(halfAngle)),
maxHalfWidthTangential = cd0.r * Math.min(
1 / (Math.sqrt(Qt * Qt + 0.5) + Qt),
ring / (Math.sqrt(aspectInv * aspectInv + ring / 2) + aspectInv)
),
tangentialTransform = {
scale: maxHalfWidthTangential * 2 / textBB.width,
rCenter: Math.cos(maxHalfWidthTangential / cd0.r) -
maxHalfWidthTangential / textAspect / cd0.r,
rotate: (180 / Math.PI * pt.midangle + 810) % 180 - 90
},
// if we need a rotated transform, pick the biggest one
// even if both are bigger than 1
rotatedTransform = tangentialTransform.scale > radialTransform.scale ?
tangentialTransform : radialTransform;
if(transform.scale < 1 && rotatedTransform.scale > transform.scale) return rotatedTransform;
return transform;
}
function getInscribedRadiusFraction(pt, cd0) {
if(pt.v === cd0.vTotal && !cd0.trace.hole) return 1;// special case of 100% with no hole
var halfAngle = Math.PI * Math.min(pt.v / cd0.vTotal, 0.5);
return Math.min(1 / (1 + 1 / Math.sin(halfAngle)), (1 - cd0.trace.hole) / 2);
}
function transformOutsideText(textBB, pt) {
var x = pt.pxmid[0],
y = pt.pxmid[1],
dx = textBB.width / 2,
dy = textBB.height / 2;
if(x < 0) dx *= -1;
if(y < 0) dy *= -1;
return {
scale: 1,
rCenter: 1,
rotate: 0,
x: dx + Math.abs(dy) * (dx > 0 ? 1 : -1) / 2,
y: dy / (1 + x * x / (y * y)),
outside: true
};
}
function scootLabels(quadrants, trace) {
var xHalf,
yHalf,
equatorFirst,
farthestX,
farthestY,
xDiffSign,
yDiffSign,
thisQuad,
oppositeQuad,
wholeSide,
i,
thisQuadOutside,
firstOppositeOutsidePt;
function topFirst(a, b) { return a.pxmid[1] - b.pxmid[1]; }
function bottomFirst(a, b) { return b.pxmid[1] - a.pxmid[1]; }
function scootOneLabel(thisPt, prevPt) {
if(!prevPt) prevPt = {};
var prevOuterY = prevPt.labelExtraY + (yHalf ? prevPt.yLabelMax : prevPt.yLabelMin),
thisInnerY = yHalf ? thisPt.yLabelMin : thisPt.yLabelMax,
thisOuterY = yHalf ? thisPt.yLabelMax : thisPt.yLabelMin,
thisSliceOuterY = thisPt.cyFinal + farthestY(thisPt.px0[1], thisPt.px1[1]),
newExtraY = prevOuterY - thisInnerY,
xBuffer,
i,
otherPt,
otherOuterY,
otherOuterX,
newExtraX;
// make sure this label doesn't overlap other labels
// this *only* has us move these labels vertically
if(newExtraY * yDiffSign > 0) thisPt.labelExtraY = newExtraY;
// make sure this label doesn't overlap any slices
if(!Array.isArray(trace.pull)) return; // this can only happen with array pulls
for(i = 0; i < wholeSide.length; i++) {
otherPt = wholeSide[i];
// overlap can only happen if the other point is pulled more than this one
if(otherPt === thisPt || ((trace.pull[thisPt.i] || 0) >= trace.pull[otherPt.i] || 0)) continue;
if((thisPt.pxmid[1] - otherPt.pxmid[1]) * yDiffSign > 0) {
// closer to the equator - by construction all of these happen first
// move the text vertically to get away from these slices
otherOuterY = otherPt.cyFinal + farthestY(otherPt.px0[1], otherPt.px1[1]);
newExtraY = otherOuterY - thisInnerY - thisPt.labelExtraY;
if(newExtraY * yDiffSign > 0) thisPt.labelExtraY += newExtraY;
} else if((thisOuterY + thisPt.labelExtraY - thisSliceOuterY) * yDiffSign > 0) {
// farther from the equator - happens after we've done all the
// vertical moving we're going to do
// move horizontally to get away from these more polar slices
// if we're moving horz. based on a slice that's several slices away from this one
// then we need some extra space for the lines to labels between them
xBuffer = 3 * xDiffSign * Math.abs(i - wholeSide.indexOf(thisPt));
otherOuterX = otherPt.cxFinal + farthestX(otherPt.px0[0], otherPt.px1[0]);
newExtraX = otherOuterX + xBuffer - (thisPt.cxFinal + thisPt.pxmid[0]) - thisPt.labelExtraX;
if(newExtraX * xDiffSign > 0) thisPt.labelExtraX += newExtraX;
}
}
}
for(yHalf = 0; yHalf < 2; yHalf++) {
equatorFirst = yHalf ? topFirst : bottomFirst;
farthestY = yHalf ? Math.max : Math.min;
yDiffSign = yHalf ? 1 : -1;
for(xHalf = 0; xHalf < 2; xHalf++) {
farthestX = xHalf ? Math.max : Math.min;
xDiffSign = xHalf ? 1 : -1;
// first sort the array
// note this is a copy of cd, so cd itself doesn't get sorted
// but we can still modify points in place.
thisQuad = quadrants[yHalf][xHalf];
thisQuad.sort(equatorFirst);
oppositeQuad = quadrants[1 - yHalf][xHalf];
wholeSide = oppositeQuad.concat(thisQuad);
thisQuadOutside = [];
for(i = 0; i < thisQuad.length; i++) {
if(thisQuad[i].yLabelMid !== undefined) thisQuadOutside.push(thisQuad[i]);
}
firstOppositeOutsidePt = false;
for(i = 0; yHalf && i < oppositeQuad.length; i++) {
if(oppositeQuad[i].yLabelMid !== undefined) {
firstOppositeOutsidePt = oppositeQuad[i];
break;
}
}
// each needs to avoid the previous
for(i = 0; i < thisQuadOutside.length; i++) {
var prevPt = i && thisQuadOutside[i - 1];
// bottom half needs to avoid the first label of the top half
// top half we still need to call scootOneLabel on the first slice
// so we can avoid other slices, but we don't pass a prevPt
if(firstOppositeOutsidePt && !i) prevPt = firstOppositeOutsidePt;
scootOneLabel(thisQuadOutside[i], prevPt);
}
}
}
}
function scalePies(cdpie, plotSize) {
var pieBoxWidth,
pieBoxHeight,
i,
j,
cd0,
trace,
tiltAxisRads,
maxPull,
scaleGroups = [],
scaleGroup,
minPxPerValUnit;
// first figure out the center and maximum radius for each pie
for(i = 0; i < cdpie.length; i++) {
cd0 = cdpie[i][0];
trace = cd0.trace;
pieBoxWidth = plotSize.w * (trace.domain.x[1] - trace.domain.x[0]);
pieBoxHeight = plotSize.h * (trace.domain.y[1] - trace.domain.y[0]);
tiltAxisRads = trace.tiltaxis * Math.PI / 180;
maxPull = trace.pull;
if(Array.isArray(maxPull)) {
maxPull = 0;
for(j = 0; j < trace.pull.length; j++) {
if(trace.pull[j] > maxPull) maxPull = trace.pull[j];
}
}
cd0.r = Math.min(
pieBoxWidth / maxExtent(trace.tilt, Math.sin(tiltAxisRads), trace.depth),
pieBoxHeight / maxExtent(trace.tilt, Math.cos(tiltAxisRads), trace.depth)
) / (2 + 2 * maxPull);
cd0.cx = plotSize.l + plotSize.w * (trace.domain.x[1] + trace.domain.x[0]) / 2;
cd0.cy = plotSize.t + plotSize.h * (2 - trace.domain.y[1] - trace.domain.y[0]) / 2;
if(trace.scalegroup && scaleGroups.indexOf(trace.scalegroup) === -1) {
scaleGroups.push(trace.scalegroup);
}
}
// Then scale any pies that are grouped
for(j = 0; j < scaleGroups.length; j++) {
minPxPerValUnit = Infinity;
scaleGroup = scaleGroups[j];
for(i = 0; i < cdpie.length; i++) {
cd0 = cdpie[i][0];
if(cd0.trace.scalegroup === scaleGroup) {
minPxPerValUnit = Math.min(minPxPerValUnit,
cd0.r * cd0.r / cd0.vTotal);
}
}
for(i = 0; i < cdpie.length; i++) {
cd0 = cdpie[i][0];
if(cd0.trace.scalegroup === scaleGroup) {
cd0.r = Math.sqrt(minPxPerValUnit * cd0.vTotal);
}
}
}
}
function setCoords(cd) {
var cd0 = cd[0],
trace = cd0.trace,
tilt = trace.tilt,
tiltAxisRads,
tiltAxisSin,
tiltAxisCos,
tiltRads,
crossTilt,
inPlane,
currentAngle = trace.rotation * Math.PI / 180,
angleFactor = 2 * Math.PI / cd0.vTotal,
firstPt = 'px0',
lastPt = 'px1',
i,
cdi,
currentCoords;
if(trace.direction === 'counterclockwise') {
for(i = 0; i < cd.length; i++) {
if(!cd[i].hidden) break; // find the first non-hidden slice
}
if(i === cd.length) return; // all slices hidden
currentAngle += angleFactor * cd[i].v;
angleFactor *= -1;
firstPt = 'px1';
lastPt = 'px0';
}
if(tilt) {
tiltRads = tilt * Math.PI / 180;
tiltAxisRads = trace.tiltaxis * Math.PI / 180;
crossTilt = Math.sin(tiltAxisRads) * Math.cos(tiltAxisRads);
inPlane = 1 - Math.cos(tiltRads);
tiltAxisSin = Math.sin(tiltAxisRads);
tiltAxisCos = Math.cos(tiltAxisRads);
}
function getCoords(angle) {
var xFlat = cd0.r * Math.sin(angle),
yFlat = -cd0.r * Math.cos(angle);
if(!tilt) return [xFlat, yFlat];
return [
xFlat * (1 - inPlane * tiltAxisSin * tiltAxisSin) + yFlat * crossTilt * inPlane,
xFlat * crossTilt * inPlane + yFlat * (1 - inPlane * tiltAxisCos * tiltAxisCos),
Math.sin(tiltRads) * (yFlat * tiltAxisCos - xFlat * tiltAxisSin)
];
}
currentCoords = getCoords(currentAngle);
for(i = 0; i < cd.length; i++) {
cdi = cd[i];
if(cdi.hidden) continue;
cdi[firstPt] = currentCoords;
currentAngle += angleFactor * cdi.v / 2;
cdi.pxmid = getCoords(currentAngle);
cdi.midangle = currentAngle;
currentAngle += angleFactor * cdi.v / 2;
currentCoords = getCoords(currentAngle);
cdi[lastPt] = currentCoords;
cdi.largeArc = (cdi.v > cd0.vTotal / 2) ? 1 : 0;
}
}
function maxExtent(tilt, tiltAxisFraction, depth) {
if(!tilt) return 1;
var sinTilt = Math.sin(tilt * Math.PI / 180);
return Math.max(0.01, // don't let it go crazy if you tilt the pie totally on its side
depth * sinTilt * Math.abs(tiltAxisFraction) +
2 * Math.sqrt(1 - sinTilt * sinTilt * tiltAxisFraction * tiltAxisFraction));
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var d3 = require('d3');
var styleOne = require('./style_one');
module.exports = function style(gd) {
gd._fullLayout._pielayer.selectAll('.trace').each(function(cd) {
var cd0 = cd[0],
trace = cd0.trace,
traceSelection = d3.select(this);
traceSelection.style({opacity: trace.opacity});
traceSelection.selectAll('.top path.surface').each(function(pt) {
d3.select(this).call(styleOne, pt, trace);
});
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
module.exports = function styleOne(s, pt, trace) {
var lineColor = trace.marker.line.color;
if(Array.isArray(lineColor)) lineColor = lineColor[pt.i] || Color.defaultLine;
var lineWidth = trace.marker.line.width || 0;
if(Array.isArray(lineWidth)) lineWidth = lineWidth[pt.i] || 0;
s.style({'stroke-width': lineWidth})
.call(Color.fill, pt.color)
.call(Color.stroke, lineColor);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 50% | (1 / 2) | 100% | (0 / 0) | 100% | (0 / 0) | 50% | (1 / 2) | |
| index.js | 18.18% | (2 / 11) | 100% | (0 / 0) | 100% | (0 / 0) | 18.18% | (2 / 11) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterglAttrs = require('../scattergl/attributes');
module.exports = {
x: scatterglAttrs.x,
y: scatterglAttrs.y,
xy: {
valType: 'data_array',
description: [
'Faster alternative to specifying `x` and `y` separately.',
'If supplied, it must be a typed `Float32Array` array that',
'represents points such that `xy[i * 2] = x[i]` and `xy[i * 2 + 1] = y[i]`'
].join(' ')
},
indices: {
valType: 'data_array',
description: [
'A sequential value, 0..n, supply it to avoid creating this array inside plotting.',
'If specified, it must be a typed `Int32Array` array.',
'Its length must be equal to or greater than the number of points.',
'For the best performance and memory use, create one large `indices` typed array',
'that is guaranteed to be at least as long as the largest number of points during',
'use, and reuse it on each `Plotly.restyle()` call.'
].join(' ')
},
xbounds: {
valType: 'data_array',
description: [
'Specify `xbounds` in the shape of `[xMin, xMax] to avoid looping through',
'the `xy` typed array. Use it in conjunction with `xy` and `ybounds` for the performance benefits.'
].join(' ')
},
ybounds: {
valType: 'data_array',
description: [
'Specify `ybounds` in the shape of `[yMin, yMax] to avoid looping through',
'the `xy` typed array. Use it in conjunction with `xy` and `xbounds` for the performance benefits.'
].join(' ')
},
text: scatterglAttrs.text,
marker: {
color: {
valType: 'color',
arrayOk: false,
role: 'style',
description: [
'Sets the marker fill color. It accepts a specific color.',
'If the color is not fully opaque and there are hundreds of thousands',
'of points, it may cause slower zooming and panning.'
].join('')
},
opacity: {
valType: 'number',
min: 0,
max: 1,
dflt: 1,
arrayOk: false,
role: 'style',
description: [
'Sets the marker opacity. The default value is `1` (fully opaque).',
'If the markers are not fully opaque and there are hundreds of thousands',
'of points, it may cause slower zooming and panning.',
'Opacity fades the color even if `blend` is left on `false` even if there',
'is no translucency effect in that case.'
].join(' ')
},
blend: {
valType: 'boolean',
dflt: null,
role: 'style',
description: [
'Determines if colors are blended together for a translucency effect',
'in case `opacity` is specified as a value less then `1`.',
'Setting `blend` to `true` reduces zoom/pan',
'speed if used with large numbers of points.'
].join(' ')
},
sizemin: {
valType: 'number',
min: 0.1,
max: 2,
dflt: 0.5,
role: 'style',
description: [
'Sets the minimum size (in px) of the rendered marker points, effective when',
'the `pointcloud` shows a million or more points.'
].join(' ')
},
sizemax: {
valType: 'number',
min: 0.1,
dflt: 20,
role: 'style',
description: [
'Sets the maximum size (in px) of the rendered marker points.',
'Effective when the `pointcloud` shows only few points.'
].join(' ')
},
border: {
color: {
valType: 'color',
arrayOk: false,
role: 'style',
description: [
'Sets the stroke color. It accepts a specific color.',
'If the color is not fully opaque and there are hundreds of thousands',
'of points, it may cause slower zooming and panning.'
].join(' ')
},
arearatio: {
valType: 'number',
min: 0,
max: 1,
dflt: 0,
role: 'style',
description: [
'Specifies what fraction of the marker area is covered with the',
'border.'
].join(' ')
}
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var pointcloud = {};
pointcloud.attributes = require('./attributes');
pointcloud.supplyDefaults = require('./defaults');
// reuse the Scatter3D 'dummy' calc step so that legends know what to do
pointcloud.calc = require('../scatter3d/calc');
pointcloud.plot = require('./convert');
pointcloud.moduleType = 'trace';
pointcloud.name = 'pointcloud';
pointcloud.basePlotModule = require('../../plots/gl2d');
pointcloud.categories = ['gl2d', 'showLegend'];
pointcloud.meta = {
description: [
'The data visualized as a point cloud set in `x` and `y`',
'using the WebGl plotting engine.'
].join(' ')
};
module.exports = pointcloud;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (8 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (8 / 8) | |
| clean_data.js | 6.67% | (1 / 15) | 0% | (0 / 15) | 0% | (0 / 1) | 7.69% | (1 / 13) | |
| colorscale_calc.js | 36.36% | (4 / 11) | 0% | (0 / 10) | 0% | (0 / 1) | 36.36% | (4 / 11) | |
| constants.js | 100% | (1 / 1) | 100% | (0 / 0) | 100% | (0 / 0) | 100% | (1 / 1) | |
| fillcolor_defaults.js | 20% | (2 / 10) | 0% | (0 / 17) | 0% | (0 / 1) | 20% | (2 / 10) | |
| get_trace_color.js | 17.65% | (3 / 17) | 0% | (0 / 46) | 0% | (0 / 1) | 17.65% | (3 / 17) | |
| index.js | 30.43% | (7 / 23) | 100% | (0 / 0) | 100% | (0 / 0) | 30.43% | (7 / 23) | |
| line_defaults.js | 25% | (3 / 12) | 0% | (0 / 12) | 0% | (0 / 1) | 27.27% | (3 / 11) | |
| line_points.js | 6.49% | (5 / 77) | 0% | (0 / 38) | 0% | (0 / 4) | 7.46% | (5 / 67) | |
| line_shape_defaults.js | 25% | (1 / 4) | 0% | (0 / 2) | 0% | (0 / 1) | 33.33% | (1 / 3) | |
| link_traces.js | 7.14% | (1 / 14) | 0% | (0 / 6) | 0% | (0 / 1) | 7.14% | (1 / 14) | |
| make_bubble_size_func.js | 22.22% | (2 / 9) | 0% | (0 / 10) | 0% | (0 / 4) | 22.22% | (2 / 9) | |
| marker_defaults.js | 17.86% | (5 / 28) | 0% | (0 / 25) | 0% | (0 / 1) | 19.23% | (5 / 26) | |
| subtypes.js | 33.33% | (2 / 6) | 0% | (0 / 11) | 0% | (0 / 4) | 33.33% | (2 / 6) | |
| text_defaults.js | 50% | (2 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 50% | (2 / 4) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | 60 60 60 60 60 60 60 60 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorAttributes = require('../../components/colorscale/color_attributes');
var errorBarAttrs = require('../../components/errorbars/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var dash = require('../../components/drawing/attributes').dash;
var Drawing = require('../../components/drawing');
var constants = require('./constants');
var extendFlat = require('../../lib/extend').extendFlat;
module.exports = {
x: {
valType: 'data_array',
description: 'Sets the x coordinates.'
},
x0: {
valType: 'any',
dflt: 0,
role: 'info',
description: [
'Alternate to `x`.',
'Builds a linear space of x coordinates.',
'Use with `dx`',
'where `x0` is the starting coordinate and `dx` the step.'
].join(' ')
},
dx: {
valType: 'number',
dflt: 1,
role: 'info',
description: [
'Sets the x coordinate step.',
'See `x0` for more info.'
].join(' ')
},
y: {
valType: 'data_array',
description: 'Sets the y coordinates.'
},
y0: {
valType: 'any',
dflt: 0,
role: 'info',
description: [
'Alternate to `y`.',
'Builds a linear space of y coordinates.',
'Use with `dy`',
'where `y0` is the starting coordinate and `dy` the step.'
].join(' ')
},
customdata: {
valType: 'data_array',
description: 'Assigns extra data to each scatter point DOM element'
},
dy: {
valType: 'number',
dflt: 1,
role: 'info',
description: [
'Sets the y coordinate step.',
'See `y0` for more info.'
].join(' ')
},
ids: {
valType: 'data_array',
description: 'A list of keys for object constancy of data points during animation'
},
text: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets text elements associated with each (x,y) pair.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
},
hovertext: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
description: [
'Sets hover text elements associated with each (x,y) pair.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
},
mode: {
valType: 'flaglist',
flags: ['lines', 'markers', 'text'],
extras: ['none'],
role: 'info',
description: [
'Determines the drawing mode for this scatter trace.',
'If the provided `mode` includes *text* then the `text` elements',
'appear at the coordinates. Otherwise, the `text` elements',
'appear on hover.',
'If there are less than ' + constants.PTS_LINESONLY + ' points,',
'then the default is *lines+markers*. Otherwise, *lines*.'
].join(' ')
},
hoveron: {
valType: 'flaglist',
flags: ['points', 'fills'],
role: 'info',
description: [
'Do the hover effects highlight individual points (markers or',
'line points) or do they highlight filled regions?',
'If the fill is *toself* or *tonext* and there are no markers',
'or text, then the default is *fills*, otherwise it is *points*.'
].join(' ')
},
line: {
color: {
valType: 'color',
role: 'style',
description: 'Sets the line color.'
},
width: {
valType: 'number',
min: 0,
dflt: 2,
role: 'style',
description: 'Sets the line width (in px).'
},
shape: {
valType: 'enumerated',
values: ['linear', 'spline', 'hv', 'vh', 'hvh', 'vhv'],
dflt: 'linear',
role: 'style',
description: [
'Determines the line shape.',
'With *spline* the lines are drawn using spline interpolation.',
'The other available values correspond to step-wise line shapes.'
].join(' ')
},
smoothing: {
valType: 'number',
min: 0,
max: 1.3,
dflt: 1,
role: 'style',
description: [
'Has an effect only if `shape` is set to *spline*',
'Sets the amount of smoothing.',
'*0* corresponds to no smoothing (equivalent to a *linear* shape).'
].join(' ')
},
dash: dash,
simplify: {
valType: 'boolean',
dflt: true,
role: 'info',
description: [
'Simplifies lines by removing nearly-collinear points. When transitioning',
'lines, it may be desirable to disable this so that the number of points',
'along the resulting SVG path is unaffected.'
].join(' ')
}
},
connectgaps: {
valType: 'boolean',
dflt: false,
role: 'info',
description: [
'Determines whether or not gaps',
'(i.e. {nan} or missing values)',
'in the provided data arrays are connected.'
].join(' ')
},
fill: {
valType: 'enumerated',
values: ['none', 'tozeroy', 'tozerox', 'tonexty', 'tonextx', 'toself', 'tonext'],
dflt: 'none',
role: 'style',
description: [
'Sets the area to fill with a solid color.',
'Use with `fillcolor` if not *none*.',
'*tozerox* and *tozeroy* fill to x=0 and y=0 respectively.',
'*tonextx* and *tonexty* fill between the endpoints of this',
'trace and the endpoints of the trace before it, connecting those',
'endpoints with straight lines (to make a stacked area graph);',
'if there is no trace before it, they behave like *tozerox* and',
'*tozeroy*.',
'*toself* connects the endpoints of the trace (or each segment',
'of the trace if it has gaps) into a closed shape.',
'*tonext* fills the space between two traces if one completely',
'encloses the other (eg consecutive contour lines), and behaves like',
'*toself* if there is no trace before it. *tonext* should not be',
'used if one trace does not enclose the other.'
].join(' ')
},
fillcolor: {
valType: 'color',
role: 'style',
description: [
'Sets the fill color.',
'Defaults to a half-transparent variant of the line color,',
'marker color, or marker line color, whichever is available.'
].join(' ')
},
marker: extendFlat({}, {
symbol: {
valType: 'enumerated',
values: Drawing.symbolList,
dflt: 'circle',
arrayOk: true,
role: 'style',
description: [
'Sets the marker symbol type.',
'Adding 100 is equivalent to appending *-open* to a symbol name.',
'Adding 200 is equivalent to appending *-dot* to a symbol name.',
'Adding 300 is equivalent to appending *-open-dot*',
'or *dot-open* to a symbol name.'
].join(' ')
},
opacity: {
valType: 'number',
min: 0,
max: 1,
arrayOk: true,
role: 'style',
description: 'Sets the marker opacity.'
},
size: {
valType: 'number',
min: 0,
dflt: 6,
arrayOk: true,
role: 'style',
description: 'Sets the marker size (in px).'
},
maxdisplayed: {
valType: 'number',
min: 0,
dflt: 0,
role: 'style',
description: [
'Sets a maximum number of points to be drawn on the graph.',
'*0* corresponds to no limit.'
].join(' ')
},
sizeref: {
valType: 'number',
dflt: 1,
role: 'style',
description: [
'Has an effect only if `marker.size` is set to a numerical array.',
'Sets the scale factor used to determine the rendered size of',
'marker points. Use with `sizemin` and `sizemode`.'
].join(' ')
},
sizemin: {
valType: 'number',
min: 0,
dflt: 0,
role: 'style',
description: [
'Has an effect only if `marker.size` is set to a numerical array.',
'Sets the minimum size (in px) of the rendered marker points.'
].join(' ')
},
sizemode: {
valType: 'enumerated',
values: ['diameter', 'area'],
dflt: 'diameter',
role: 'info',
description: [
'Has an effect only if `marker.size` is set to a numerical array.',
'Sets the rule for which the data in `size` is converted',
'to pixels.'
].join(' ')
},
showscale: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Has an effect only if `marker.color` is set to a numerical array.',
'Determines whether or not a colorbar is displayed.'
].join(' ')
},
colorbar: colorbarAttrs,
line: extendFlat({}, {
width: {
valType: 'number',
min: 0,
arrayOk: true,
role: 'style',
description: 'Sets the width (in px) of the lines bounding the marker points.'
}
},
colorAttributes('marker.line')
)
},
colorAttributes('marker')
),
textposition: {
valType: 'enumerated',
values: [
'top left', 'top center', 'top right',
'middle left', 'middle center', 'middle right',
'bottom left', 'bottom center', 'bottom right'
],
dflt: 'middle center',
arrayOk: true,
role: 'style',
description: [
'Sets the positions of the `text` elements',
'with respects to the (x,y) coordinates.'
].join(' ')
},
textfont: {
family: {
valType: 'string',
role: 'style',
noBlank: true,
strict: true,
arrayOk: true
},
size: {
valType: 'number',
role: 'style',
min: 1,
arrayOk: true
},
color: {
valType: 'color',
role: 'style',
arrayOk: true
},
description: 'Sets the text font.'
},
r: {
valType: 'data_array',
description: [
'For polar chart only.',
'Sets the radial coordinates.'
].join('')
},
t: {
valType: 'data_array',
description: [
'For polar chart only.',
'Sets the angular coordinates.'
].join('')
},
error_y: errorBarAttrs,
error_x: errorBarAttrs
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // remove opacity for any trace that has a fill or is filled to module.exports = function cleanData(fullData) { for(var i = 0; i < fullData.length; i++) { var tracei = fullData[i]; if(tracei.type !== 'scatter') continue; var filli = tracei.fill; if(filli === 'none' || filli === 'toself') continue; tracei.opacity = undefined; if(filli === 'tonexty' || filli === 'tonextx') { for(var j = i - 1; j >= 0; j--) { var tracej = fullData[j]; if((tracej.type === 'scatter') && (tracej.xaxis === tracei.xaxis) && (tracej.yaxis === tracei.yaxis)) { tracej.opacity = undefined; break; } } } } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var hasColorscale = require('../../components/colorscale/has_colorscale');
var calcColorscale = require('../../components/colorscale/calc');
var subTypes = require('./subtypes');
module.exports = function calcMarkerColorscale(trace) {
if(subTypes.hasLines(trace) && hasColorscale(trace, 'line')) {
calcColorscale(trace, trace.line.color, 'line', 'c');
}
if(subTypes.hasMarkers(trace)) {
if(hasColorscale(trace, 'marker')) {
calcColorscale(trace, trace.marker.color, 'marker', 'c');
}
if(hasColorscale(trace, 'marker.line')) {
calcColorscale(trace, trace.marker.line.color, 'marker.line', 'c');
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
module.exports = {
PTS_LINESONLY: 20
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
module.exports = function fillColorDefaults(traceIn, traceOut, defaultColor, coerce) {
var inheritColorFromMarker = false;
if(traceOut.marker) {
// don't try to inherit a color array
var markerColor = traceOut.marker.color,
markerLineColor = (traceOut.marker.line || {}).color;
if(markerColor && !Array.isArray(markerColor)) {
inheritColorFromMarker = markerColor;
}
else if(markerLineColor && !Array.isArray(markerLineColor)) {
inheritColorFromMarker = markerLineColor;
}
}
coerce('fillcolor', Color.addOpacity(
(traceOut.line || {}).color ||
inheritColorFromMarker ||
defaultColor, 0.5
));
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
var subtypes = require('./subtypes');
module.exports = function getTraceColor(trace, di) {
var lc, tc;
// TODO: text modes
if(trace.mode === 'lines') {
lc = trace.line.color;
return (lc && Color.opacity(lc)) ?
lc : trace.fillcolor;
}
else if(trace.mode === 'none') {
return trace.fill ? trace.fillcolor : '';
}
else {
var mc = di.mcc || (trace.marker || {}).color,
mlc = di.mlcc || ((trace.marker || {}).line || {}).color;
tc = (mc && Color.opacity(mc)) ? mc :
(mlc && Color.opacity(mlc) &&
(di.mlw || ((trace.marker || {}).line || {}).width)) ? mlc : '';
if(tc) {
// make sure the points aren't TOO transparent
if(Color.opacity(tc) < 0.3) {
return Color.addOpacity(tc, 0.3);
}
else return tc;
}
else {
lc = (trace.line || {}).color;
return (lc && Color.opacity(lc) &&
subtypes.hasLines(trace) && trace.line.width) ?
lc : trace.fillcolor;
}
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 2 2 2 2 2 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Scatter = {};
var subtypes = require('./subtypes');
Scatter.hasLines = subtypes.hasLines;
Scatter.hasMarkers = subtypes.hasMarkers;
Scatter.hasText = subtypes.hasText;
Scatter.isBubble = subtypes.isBubble;
// traces with < this many points are by default shown
// with points and lines, > just get lines
Scatter.attributes = require('./attributes');
Scatter.supplyDefaults = require('./defaults');
Scatter.cleanData = require('./clean_data');
Scatter.calc = require('./calc');
Scatter.arraysToCalcdata = require('./arrays_to_calcdata');
Scatter.plot = require('./plot');
Scatter.colorbar = require('./colorbar');
Scatter.style = require('./style');
Scatter.hoverPoints = require('./hover');
Scatter.selectPoints = require('./select');
Scatter.animatable = true;
Scatter.moduleType = 'trace';
Scatter.name = 'scatter';
Scatter.basePlotModule = require('../../plots/cartesian');
Scatter.categories = ['cartesian', 'symbols', 'markerColorscale', 'errorBarsOK', 'showLegend'];
Scatter.meta = {
description: [
'The scatter trace type encompasses line charts, scatter charts, text charts, and bubble charts.',
'The data visualized as scatter point or lines is set in `x` and `y`.',
'Text (appearing either on the chart or on hover only) is via `text`.',
'Bubble charts are achieved by setting `marker.size` and/or `marker.color`',
'to numerical arrays.'
].join(' ')
};
module.exports = Scatter;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var hasColorscale = require('../../components/colorscale/has_colorscale');
var colorscaleDefaults = require('../../components/colorscale/defaults');
module.exports = function lineDefaults(traceIn, traceOut, defaultColor, layout, coerce, opts) {
var markerColor = (traceIn.marker || {}).color;
coerce('line.color', defaultColor);
if(hasColorscale(traceIn, 'line')) {
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'line.', cLetter: 'c'});
}
else {
var lineColorDflt = (Array.isArray(markerColor) ? false : markerColor) || defaultColor;
coerce('line.color', lineColorDflt);
}
coerce('line.width');
if(!(opts || {}).noDash) coerce('line.dash');
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var BADNUM = require('../../constants/numerical').BADNUM;
module.exports = function linePoints(d, opts) {
var xa = opts.xaxis,
ya = opts.yaxis,
simplify = opts.simplify,
connectGaps = opts.connectGaps,
baseTolerance = opts.baseTolerance,
linear = opts.linear,
segments = [],
minTolerance = 0.2, // fraction of tolerance "so close we don't even consider it a new point"
pts = new Array(d.length),
pti = 0,
i,
// pt variables are pixel coordinates [x,y] of one point
clusterStartPt, // these four are the outputs of clustering on a line
clusterEndPt,
clusterHighPt,
clusterLowPt,
thisPt, // "this" is the next point we're considering adding to the cluster
clusterRefDist,
clusterHighFirst, // did we encounter the high point first, then a low point, or vice versa?
clusterUnitVector, // the first two points in the cluster determine its unit vector
// so the second is always in the "High" direction
thisVector, // the pixel delta from clusterStartPt
// val variables are (signed) pixel distances along the cluster vector
clusterHighVal,
clusterLowVal,
thisVal,
// deviation variables are (signed) pixel distances normal to the cluster vector
clusterMinDeviation,
clusterMaxDeviation,
thisDeviation;
if(!simplify) {
baseTolerance = minTolerance = -1;
}
// turn one calcdata point into pixel coordinates
function getPt(index) {
var x = xa.c2p(d[index].x),
y = ya.c2p(d[index].y);
if(x === BADNUM || y === BADNUM) return false;
return [x, y];
}
// if we're off-screen, increase tolerance over baseTolerance
function getTolerance(pt) {
var xFrac = pt[0] / xa._length,
yFrac = pt[1] / ya._length;
return (1 + 10 * Math.max(0, -xFrac, xFrac - 1, -yFrac, yFrac - 1)) * baseTolerance;
}
function ptDist(pt1, pt2) {
var dx = pt1[0] - pt2[0],
dy = pt1[1] - pt2[1];
return Math.sqrt(dx * dx + dy * dy);
}
// loop over ALL points in this trace
for(i = 0; i < d.length; i++) {
clusterStartPt = getPt(i);
if(!clusterStartPt) continue;
pti = 0;
pts[pti++] = clusterStartPt;
// loop over one segment of the trace
for(i++; i < d.length; i++) {
clusterHighPt = getPt(i);
if(!clusterHighPt) {
if(connectGaps) continue;
else break;
}
// can't decimate if nonlinear line shape
// TODO: we *could* decimate [hv]{2,3} shapes if we restricted clusters to horz or vert again
// but spline would be verrry awkward to decimate
if(!linear) {
pts[pti++] = clusterHighPt;
continue;
}
clusterRefDist = ptDist(clusterHighPt, clusterStartPt);
if(clusterRefDist < getTolerance(clusterHighPt) * minTolerance) continue;
clusterUnitVector = [
(clusterHighPt[0] - clusterStartPt[0]) / clusterRefDist,
(clusterHighPt[1] - clusterStartPt[1]) / clusterRefDist
];
clusterLowPt = clusterStartPt;
clusterHighVal = clusterRefDist;
clusterLowVal = clusterMinDeviation = clusterMaxDeviation = 0;
clusterHighFirst = false;
clusterEndPt = clusterHighPt;
// loop over one cluster of points that collapse onto one line
for(i++; i < d.length; i++) {
thisPt = getPt(i);
if(!thisPt) {
if(connectGaps) continue;
else break;
}
thisVector = [
thisPt[0] - clusterStartPt[0],
thisPt[1] - clusterStartPt[1]
];
// cross product (or dot with normal to the cluster vector)
thisDeviation = thisVector[0] * clusterUnitVector[1] - thisVector[1] * clusterUnitVector[0];
clusterMinDeviation = Math.min(clusterMinDeviation, thisDeviation);
clusterMaxDeviation = Math.max(clusterMaxDeviation, thisDeviation);
if(clusterMaxDeviation - clusterMinDeviation > getTolerance(thisPt)) break;
clusterEndPt = thisPt;
thisVal = thisVector[0] * clusterUnitVector[0] + thisVector[1] * clusterUnitVector[1];
if(thisVal > clusterHighVal) {
clusterHighVal = thisVal;
clusterHighPt = thisPt;
clusterHighFirst = false;
} else if(thisVal < clusterLowVal) {
clusterLowVal = thisVal;
clusterLowPt = thisPt;
clusterHighFirst = true;
}
}
// insert this cluster into pts
// we've already inserted the start pt, now check if we have high and low pts
if(clusterHighFirst) {
pts[pti++] = clusterHighPt;
if(clusterEndPt !== clusterLowPt) pts[pti++] = clusterLowPt;
} else {
if(clusterLowPt !== clusterStartPt) pts[pti++] = clusterLowPt;
if(clusterEndPt !== clusterHighPt) pts[pti++] = clusterHighPt;
}
// and finally insert the end pt
pts[pti++] = clusterEndPt;
// have we reached the end of this segment?
if(i >= d.length || !thisPt) break;
// otherwise we have an out-of-cluster point to insert as next clusterStartPt
pts[pti++] = thisPt;
clusterStartPt = thisPt;
}
segments.push(pts.slice(0, pti));
}
return segments;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; // common to 'scatter' and 'scatterternary' module.exports = function handleLineShapeDefaults(traceIn, traceOut, coerce) { var shape = coerce('line.shape'); if(shape === 'spline') coerce('line.smoothing'); }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 1 | /** * Copyright 2012-2017, Plotly, Inc. * All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; module.exports = function linkTraces(gd, plotinfo, cdscatter) { var cd, trace; var prevtrace = null; for(var i = 0; i < cdscatter.length; ++i) { cd = cdscatter[i]; trace = cd[0].trace; // Note: The check which ensures all cdscatter here are for the same axis and // are either cartesian or scatterternary has been removed. This code assumes // the passed scattertraces have been filtered to the proper plot types and // the proper subplots. if(trace.visible === true) { trace._nexttrace = null; if(['tonextx', 'tonexty', 'tonext'].indexOf(trace.fill) !== -1) { trace._prevtrace = prevtrace; if(prevtrace) { prevtrace._nexttrace = trace; } } prevtrace = trace; } else { trace._prevtrace = trace._nexttrace = null; } } }; |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
// used in the drawing step for 'scatter' and 'scattegeo' and
// in the convert step for 'scatter3d'
module.exports = function makeBubbleSizeFn(trace) {
var marker = trace.marker,
sizeRef = marker.sizeref || 1,
sizeMin = marker.sizemin || 0;
// for bubble charts, allow scaling the provided value linearly
// and by area or diameter.
// Note this only applies to the array-value sizes
var baseFn = (marker.sizemode === 'area') ?
function(v) { return Math.sqrt(v / sizeRef); } :
function(v) { return v / sizeRef; };
// TODO add support for position/negative bubbles?
// TODO add 'sizeoffset' attribute?
return function(v) {
var baseSize = baseFn(v / 2);
// don't show non-numeric and negative sizes
return (isNumeric(baseSize) && (baseSize > 0)) ?
Math.max(baseSize, sizeMin) :
0;
};
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
var hasColorscale = require('../../components/colorscale/has_colorscale');
var colorscaleDefaults = require('../../components/colorscale/defaults');
var subTypes = require('./subtypes');
module.exports = function markerDefaults(traceIn, traceOut, defaultColor, layout, coerce, opts) {
var isBubble = subTypes.isBubble(traceIn),
lineColor = (traceIn.line || {}).color,
defaultMLC;
// marker.color inherit from line.color (even if line.color is an array)
if(lineColor) defaultColor = lineColor;
coerce('marker.symbol');
coerce('marker.opacity', isBubble ? 0.7 : 1);
coerce('marker.size');
coerce('marker.color', defaultColor);
if(hasColorscale(traceIn, 'marker')) {
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.', cLetter: 'c'});
}
if(!(opts || {}).noLine) {
// if there's a line with a different color than the marker, use
// that line color as the default marker line color
// (except when it's an array)
// mostly this is for transparent markers to behave nicely
if(lineColor && !Array.isArray(lineColor) && (traceOut.marker.color !== lineColor)) {
defaultMLC = lineColor;
}
else if(isBubble) defaultMLC = Color.background;
else defaultMLC = Color.defaultLine;
coerce('marker.line.color', defaultMLC);
if(hasColorscale(traceIn, 'marker.line')) {
colorscaleDefaults(traceIn, traceOut, layout, coerce, {prefix: 'marker.line.', cLetter: 'c'});
}
coerce('marker.line.width', isBubble ? 1 : 0);
}
if(isBubble) {
coerce('marker.sizeref');
coerce('marker.sizemin');
coerce('marker.sizemode');
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
module.exports = {
hasLines: function(trace) {
return trace.visible && trace.mode &&
trace.mode.indexOf('lines') !== -1;
},
hasMarkers: function(trace) {
return trace.visible && trace.mode &&
trace.mode.indexOf('markers') !== -1;
},
hasText: function(trace) {
return trace.visible && trace.mode &&
trace.mode.indexOf('text') !== -1;
},
isBubble: function(trace) {
return Lib.isPlainObject(trace.marker) &&
Array.isArray(trace.marker.size);
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
// common to 'scatter', 'scatter3d' and 'scattergeo'
module.exports = function(traceIn, traceOut, layout, coerce) {
coerce('textposition');
Lib.coerceFont(coerce, 'textfont', layout.font);
};
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 20% | (2 / 10) | 100% | (0 / 0) | 0% | (0 / 1) | 20% | (2 / 10) | |
| calc_errors.js | 16.67% | (5 / 30) | 0% | (0 / 10) | 0% | (0 / 3) | 18.52% | (5 / 27) | |
| convert.js | 10.76% | (27 / 251) | 0% | (0 / 119) | 0% | (0 / 14) | 11.44% | (27 / 236) | |
| index.js | 23.08% | (3 / 13) | 100% | (0 / 0) | 100% | (0 / 0) | 23.08% | (3 / 13) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | 2 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var errorBarAttrs = require('../../components/errorbars/attributes');
var DASHES = require('../../constants/gl3d_dashes');
var MARKER_SYMBOLS = require('../../constants/gl_markers');
var extendFlat = require('../../lib/extend').extendFlat;
var scatterLineAttrs = scatterAttrs.line,
scatterMarkerAttrs = scatterAttrs.marker,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
function makeProjectionAttr(axLetter) {
return {
show: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Sets whether or not projections are shown along the',
axLetter, 'axis.'
].join(' ')
},
opacity: {
valType: 'number',
role: 'style',
min: 0,
max: 1,
dflt: 1,
description: 'Sets the projection color.'
},
scale: {
valType: 'number',
role: 'style',
min: 0,
max: 10,
dflt: 2 / 3,
description: [
'Sets the scale factor determining the size of the',
'projection marker points.'
].join(' ')
}
};
}
module.exports = {
x: {
valType: 'data_array',
description: 'Sets the x coordinates.'
},
y: {
valType: 'data_array',
description: 'Sets the y coordinates.'
},
z: {
valType: 'data_array',
description: 'Sets the z coordinates.'
},
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (x,y,z) triplet.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y,z) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets text elements associated with each (x,y,z) triplet.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y,z) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
mode: extendFlat({}, scatterAttrs.mode, // shouldn't this be on-par with 2D?
{dflt: 'lines+markers'}),
surfaceaxis: {
valType: 'enumerated',
role: 'info',
values: [-1, 0, 1, 2],
dflt: -1,
description: [
'If *-1*, the scatter points are not fill with a surface',
'If *0*, *1*, *2*, the scatter points are filled with',
'a Delaunay surface about the x, y, z respectively.'
].join(' ')
},
surfacecolor: {
valType: 'color',
role: 'style',
description: 'Sets the surface fill color.'
},
projection: {
x: makeProjectionAttr('x'),
y: makeProjectionAttr('y'),
z: makeProjectionAttr('z')
},
connectgaps: scatterAttrs.connectgaps,
line: extendFlat({}, {
width: scatterLineAttrs.width,
dash: {
valType: 'enumerated',
values: Object.keys(DASHES),
dflt: 'solid',
role: 'style',
description: 'Sets the dash style of the lines.'
},
showscale: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Has an effect only if `line.color` is set to a numerical array.',
'Determines whether or not a colorbar is displayed.'
].join(' ')
}
},
colorAttributes('line')
),
marker: extendFlat({}, { // Parity with scatter.js?
symbol: {
valType: 'enumerated',
values: Object.keys(MARKER_SYMBOLS),
role: 'style',
dflt: 'circle',
arrayOk: true,
description: 'Sets the marker symbol type.'
},
size: extendFlat({}, scatterMarkerAttrs.size, {dflt: 8}),
sizeref: scatterMarkerAttrs.sizeref,
sizemin: scatterMarkerAttrs.sizemin,
sizemode: scatterMarkerAttrs.sizemode,
opacity: extendFlat({}, scatterMarkerAttrs.opacity, {
arrayOk: false,
description: [
'Sets the marker opacity.',
'Note that the marker opacity for scatter3d traces',
'must be a scalar value for performance reasons.',
'To set a blending opacity value',
'(i.e. which is not transparent), set *marker.color*',
'to an rgba color and use its alpha channel.'
].join(' ')
}),
showscale: scatterMarkerAttrs.showscale,
colorbar: scatterMarkerAttrs.colorbar,
line: extendFlat({},
{width: extendFlat({}, scatterMarkerLineAttrs.width, {arrayOk: false})},
colorAttributes('marker.line')
)
},
colorAttributes('marker')
),
textposition: extendFlat({}, scatterAttrs.textposition, {dflt: 'top center'}),
textfont: scatterAttrs.textfont,
error_x: errorBarAttrs,
error_y: errorBarAttrs,
error_z: errorBarAttrs,
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var makeComputeError = require('../../components/errorbars/compute_error');
function calculateAxisErrors(data, params, scaleFactor) {
if(!params || !params.visible) return null;
var computeError = makeComputeError(params);
var result = new Array(data.length);
for(var i = 0; i < data.length; i++) {
var errors = computeError(+data[i], i);
result[i] = [
-errors[0] * scaleFactor,
errors[1] * scaleFactor
];
}
return result;
}
function dataLength(array) {
for(var i = 0; i < array.length; i++) {
if(array[i]) return array[i].length;
}
return 0;
}
function calculateErrors(data, scaleFactor) {
var errors = [
calculateAxisErrors(data.x, data.error_x, scaleFactor[0]),
calculateAxisErrors(data.y, data.error_y, scaleFactor[1]),
calculateAxisErrors(data.z, data.error_z, scaleFactor[2])
];
var n = dataLength(errors);
if(n === 0) return null;
var errorBounds = new Array(n);
for(var i = 0; i < n; i++) {
var bound = [[0, 0, 0], [0, 0, 0]];
for(var j = 0; j < 3; j++) {
if(errors[j]) {
for(var k = 0; k < 2; k++) {
bound[k][j] = errors[j][i][k];
}
}
}
errorBounds[i] = bound;
}
return errorBounds;
}
module.exports = calculateErrors;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createLinePlot = require('gl-line3d');
var createScatterPlot = require('gl-scatter3d');
var createErrorBars = require('gl-error3d');
var createMesh = require('gl-mesh3d');
var triangulate = require('delaunay-triangulate');
var Lib = require('../../lib');
var str2RgbaArray = require('../../lib/str2rgbarray');
var formatColor = require('../../lib/gl_format_color');
var makeBubbleSizeFn = require('../scatter/make_bubble_size_func');
var DASH_PATTERNS = require('../../constants/gl3d_dashes');
var MARKER_SYMBOLS = require('../../constants/gl_markers');
var calculateError = require('./calc_errors');
function LineWithMarkers(scene, uid) {
this.scene = scene;
this.uid = uid;
this.linePlot = null;
this.scatterPlot = null;
this.errorBars = null;
this.textMarkers = null;
this.delaunayMesh = null;
this.color = null;
this.mode = '';
this.dataPoints = [];
this.axesBounds = [
[-Infinity, -Infinity, -Infinity],
[Infinity, Infinity, Infinity]
];
this.textLabels = null;
this.data = null;
}
var proto = LineWithMarkers.prototype;
proto.handlePick = function(selection) {
if(selection.object &&
(selection.object === this.linePlot ||
selection.object === this.delaunayMesh ||
selection.object === this.textMarkers ||
selection.object === this.scatterPlot)) {
if(selection.object.highlight) {
selection.object.highlight(null);
}
if(this.scatterPlot) {
selection.object = this.scatterPlot;
this.scatterPlot.highlight(selection.data);
}
if(this.textLabels) {
if(this.textLabels[selection.data.index] !== undefined) {
selection.textLabel = this.textLabels[selection.data.index];
} else {
selection.textLabel = this.textLabels;
}
}
else selection.textLabel = '';
var selectIndex = selection.data.index;
selection.traceCoordinate = [
this.data.x[selectIndex],
this.data.y[selectIndex],
this.data.z[selectIndex]
];
return true;
}
};
function constructDelaunay(points, color, axis) {
var u = (axis + 1) % 3;
var v = (axis + 2) % 3;
var filteredPoints = [];
var filteredIds = [];
var i;
for(i = 0; i < points.length; ++i) {
var p = points[i];
if(isNaN(p[u]) || !isFinite(p[u]) ||
isNaN(p[v]) || !isFinite(p[v])) {
continue;
}
filteredPoints.push([p[u], p[v]]);
filteredIds.push(i);
}
var cells = triangulate(filteredPoints);
for(i = 0; i < cells.length; ++i) {
var c = cells[i];
for(var j = 0; j < c.length; ++j) {
c[j] = filteredIds[c[j]];
}
}
return {
positions: points,
cells: cells,
meshColor: color
};
}
function calculateErrorParams(errors) {
var capSize = [0.0, 0.0, 0.0],
color = [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
lineWidth = [0.0, 0.0, 0.0];
for(var i = 0; i < 3; i++) {
var e = errors[i];
if(e && e.copy_zstyle !== false) e = errors[2];
if(!e) continue;
capSize[i] = e.width / 2; // ballpark rescaling
color[i] = str2RgbaArray(e.color);
lineWidth = e.thickness;
}
return {capSize: capSize, color: color, lineWidth: lineWidth};
}
function calculateTextOffset(tp) {
// Read out text properties
var textOffset = [0, 0];
if(Array.isArray(tp)) return [0, -1];
if(tp.indexOf('bottom') >= 0) textOffset[1] += 1;
if(tp.indexOf('top') >= 0) textOffset[1] -= 1;
if(tp.indexOf('left') >= 0) textOffset[0] -= 1;
if(tp.indexOf('right') >= 0) textOffset[0] += 1;
return textOffset;
}
function calculateSize(sizeIn, sizeFn) {
// rough parity with Plotly 2D markers
return sizeFn(sizeIn * 4);
}
function calculateSymbol(symbolIn) {
return MARKER_SYMBOLS[symbolIn];
}
function formatParam(paramIn, len, calculate, dflt, extraFn) {
var paramOut = null;
if(Array.isArray(paramIn)) {
paramOut = [];
for(var i = 0; i < len; i++) {
if(paramIn[i] === undefined) paramOut[i] = dflt;
else paramOut[i] = calculate(paramIn[i], extraFn);
}
}
else paramOut = calculate(paramIn, Lib.identity);
return paramOut;
}
function convertPlotlyOptions(scene, data) {
var params, i,
points = [],
sceneLayout = scene.fullSceneLayout,
scaleFactor = scene.dataScale,
xaxis = sceneLayout.xaxis,
yaxis = sceneLayout.yaxis,
zaxis = sceneLayout.zaxis,
marker = data.marker,
line = data.line,
xc, x = data.x || [],
yc, y = data.y || [],
zc, z = data.z || [],
len = x.length,
xcalendar = data.xcalendar,
ycalendar = data.ycalendar,
zcalendar = data.zcalendar,
text;
// Convert points
for(i = 0; i < len; i++) {
// sanitize numbers and apply transforms based on axes.type
xc = xaxis.d2l(x[i], 0, xcalendar) * scaleFactor[0];
yc = yaxis.d2l(y[i], 0, ycalendar) * scaleFactor[1];
zc = zaxis.d2l(z[i], 0, zcalendar) * scaleFactor[2];
points[i] = [xc, yc, zc];
}
// convert text
if(Array.isArray(data.text)) text = data.text;
else if(data.text !== undefined) {
text = new Array(len);
for(i = 0; i < len; i++) text[i] = data.text;
}
// Build object parameters
params = {
position: points,
mode: data.mode,
text: text
};
if('line' in data) {
params.lineColor = formatColor(line, 1, len);
params.lineWidth = line.width;
params.lineDashes = line.dash;
}
if('marker' in data) {
var sizeFn = makeBubbleSizeFn(data);
params.scatterColor = formatColor(marker, 1, len);
params.scatterSize = formatParam(marker.size, len, calculateSize, 20, sizeFn);
params.scatterMarker = formatParam(marker.symbol, len, calculateSymbol, '●');
params.scatterLineWidth = marker.line.width; // arrayOk === false
params.scatterLineColor = formatColor(marker.line, 1, len);
params.scatterAngle = 0;
}
if('textposition' in data) {
params.textOffset = calculateTextOffset(data.textposition); // arrayOk === false
params.textColor = formatColor(data.textfont, 1, len);
params.textSize = formatParam(data.textfont.size, len, Lib.identity, 12);
params.textFont = data.textfont.family; // arrayOk === false
params.textAngle = 0;
}
var dims = ['x', 'y', 'z'];
params.project = [false, false, false];
params.projectScale = [1, 1, 1];
params.projectOpacity = [1, 1, 1];
for(i = 0; i < 3; ++i) {
var projection = data.projection[dims[i]];
if((params.project[i] = projection.show)) {
params.projectOpacity[i] = projection.opacity;
params.projectScale[i] = projection.scale;
}
}
params.errorBounds = calculateError(data, scaleFactor);
var errorParams = calculateErrorParams([data.error_x, data.error_y, data.error_z]);
params.errorColor = errorParams.color;
params.errorLineWidth = errorParams.lineWidth;
params.errorCapSize = errorParams.capSize;
params.delaunayAxis = data.surfaceaxis;
params.delaunayColor = str2RgbaArray(data.surfacecolor);
return params;
}
function arrayToColor(color) {
if(Array.isArray(color)) {
var c = color[0];
if(Array.isArray(c)) color = c;
return 'rgb(' + color.slice(0, 3).map(function(x) {
return Math.round(x * 255);
}) + ')';
}
return null;
}
proto.update = function(data) {
var gl = this.scene.glplot.gl,
lineOptions,
scatterOptions,
errorOptions,
textOptions,
dashPattern = DASH_PATTERNS.solid;
// Save data
this.data = data;
// Run data conversion
var options = convertPlotlyOptions(this.scene, data);
if('mode' in options) {
this.mode = options.mode;
}
if('lineDashes' in options) {
if(options.lineDashes in DASH_PATTERNS) {
dashPattern = DASH_PATTERNS[options.lineDashes];
}
}
this.color = arrayToColor(options.scatterColor) ||
arrayToColor(options.lineColor);
// Save data points
this.dataPoints = options.position;
lineOptions = {
gl: gl,
position: options.position,
color: options.lineColor,
lineWidth: options.lineWidth || 1,
dashes: dashPattern[0],
dashScale: dashPattern[1],
opacity: data.opacity,
connectGaps: data.connectgaps
};
if(this.mode.indexOf('lines') !== -1) {
if(this.linePlot) this.linePlot.update(lineOptions);
else {
this.linePlot = createLinePlot(lineOptions);
this.linePlot._trace = this;
this.scene.glplot.add(this.linePlot);
}
} else if(this.linePlot) {
this.scene.glplot.remove(this.linePlot);
this.linePlot.dispose();
this.linePlot = null;
}
// N.B. marker.opacity must be a scalar for performance
var scatterOpacity = data.opacity;
if(data.marker && data.marker.opacity) scatterOpacity *= data.marker.opacity;
scatterOptions = {
gl: gl,
position: options.position,
color: options.scatterColor,
size: options.scatterSize,
glyph: options.scatterMarker,
opacity: scatterOpacity,
orthographic: true,
lineWidth: options.scatterLineWidth,
lineColor: options.scatterLineColor,
project: options.project,
projectScale: options.projectScale,
projectOpacity: options.projectOpacity
};
if(this.mode.indexOf('markers') !== -1) {
if(this.scatterPlot) this.scatterPlot.update(scatterOptions);
else {
this.scatterPlot = createScatterPlot(scatterOptions);
this.scatterPlot._trace = this;
this.scatterPlot.highlightScale = 1;
this.scene.glplot.add(this.scatterPlot);
}
} else if(this.scatterPlot) {
this.scene.glplot.remove(this.scatterPlot);
this.scatterPlot.dispose();
this.scatterPlot = null;
}
textOptions = {
gl: gl,
position: options.position,
glyph: options.text,
color: options.textColor,
size: options.textSize,
angle: options.textAngle,
alignment: options.textOffset,
font: options.textFont,
orthographic: true,
lineWidth: 0,
project: false,
opacity: data.opacity
};
this.textLabels = data.hovertext || data.text;
if(this.mode.indexOf('text') !== -1) {
if(this.textMarkers) this.textMarkers.update(textOptions);
else {
this.textMarkers = createScatterPlot(textOptions);
this.textMarkers._trace = this;
this.textMarkers.highlightScale = 1;
this.scene.glplot.add(this.textMarkers);
}
} else if(this.textMarkers) {
this.scene.glplot.remove(this.textMarkers);
this.textMarkers.dispose();
this.textMarkers = null;
}
errorOptions = {
gl: gl,
position: options.position,
color: options.errorColor,
error: options.errorBounds,
lineWidth: options.errorLineWidth,
capSize: options.errorCapSize,
opacity: data.opacity
};
if(this.errorBars) {
if(options.errorBounds) {
this.errorBars.update(errorOptions);
} else {
this.scene.glplot.remove(this.errorBars);
this.errorBars.dispose();
this.errorBars = null;
}
} else if(options.errorBounds) {
this.errorBars = createErrorBars(errorOptions);
this.errorBars._trace = this;
this.scene.glplot.add(this.errorBars);
}
if(options.delaunayAxis >= 0) {
var delaunayOptions = constructDelaunay(
options.position,
options.delaunayColor,
options.delaunayAxis
);
delaunayOptions.opacity = data.opacity;
if(this.delaunayMesh) {
this.delaunayMesh.update(delaunayOptions);
} else {
delaunayOptions.gl = gl;
this.delaunayMesh = createMesh(delaunayOptions);
this.delaunayMesh._trace = this;
this.scene.glplot.add(this.delaunayMesh);
}
} else if(this.delaunayMesh) {
this.scene.glplot.remove(this.delaunayMesh);
this.delaunayMesh.dispose();
this.delaunayMesh = null;
}
};
proto.dispose = function() {
if(this.linePlot) {
this.scene.glplot.remove(this.linePlot);
this.linePlot.dispose();
}
if(this.scatterPlot) {
this.scene.glplot.remove(this.scatterPlot);
this.scatterPlot.dispose();
}
if(this.errorBars) {
this.scene.glplot.remove(this.errorBars);
this.errorBars.dispose();
}
if(this.textMarkers) {
this.scene.glplot.remove(this.textMarkers);
this.textMarkers.dispose();
}
if(this.delaunayMesh) {
this.scene.glplot.remove(this.delaunayMesh);
this.delaunayMesh.dispose();
}
};
function createLineWithMarkers(scene, data) {
var plot = new LineWithMarkers(scene, data.uid);
plot.update(data);
return plot;
}
module.exports = createLineWithMarkers;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 2 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Scatter3D = {};
Scatter3D.plot = require('./convert');
Scatter3D.attributes = require('./attributes');
Scatter3D.markerSymbols = require('../../constants/gl_markers');
Scatter3D.supplyDefaults = require('./defaults');
Scatter3D.colorbar = require('../scatter/colorbar');
Scatter3D.calc = require('./calc');
Scatter3D.moduleType = 'trace';
Scatter3D.name = 'scatter3d';
Scatter3D.basePlotModule = require('../../plots/gl3d');
Scatter3D.categories = ['gl3d', 'symbols', 'markerColorscale', 'showLegend'];
Scatter3D.meta = {
hrName: 'scatter_3d',
description: [
'The data visualized as scatter point or lines in 3D dimension',
'is set in `x`, `y`, `z`.',
'Text (appearing either on the chart or on hover only) is via `text`.',
'Bubble charts are achieved by setting `marker.size` and/or `marker.color`',
'Projections are achieved via `projection`.',
'Surface fills are achieved via `surfaceaxis`.'
].join(' ')
};
module.exports = Scatter3D;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 14.29% | (1 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (1 / 7) | |
| index.js | 13.33% | (2 / 15) | 100% | (0 / 0) | 100% | (0 / 0) | 13.33% | (2 / 15) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var scatterMarkerAttrs = scatterAttrs.marker,
scatterLineAttrs = scatterAttrs.line,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
module.exports = {
carpet: {
valType: 'string',
role: 'info',
description: [
'An identifier for this carpet, so that `scattercarpet` and',
'`scattercontour` traces can specify a carpet plot on which',
'they lie'
].join(' ')
},
a: {
valType: 'data_array',
description: [
'Sets the quantity of component `a` in each data point.',
'If `a`, `b`, and `c` are all provided, they need not be',
'normalized, only the relative values matter. If only two',
'arrays are provided they must be normalized to match',
'`ternary<i>.sum`.'
].join(' ')
},
b: {
valType: 'data_array',
description: [
'Sets the quantity of component `a` in each data point.',
'If `a`, `b`, and `c` are all provided, they need not be',
'normalized, only the relative values matter. If only two',
'arrays are provided they must be normalized to match',
'`ternary<i>.sum`.'
].join(' ')
},
sum: {
valType: 'number',
role: 'info',
dflt: 0,
min: 0,
description: [
'The number each triplet should sum to,',
'if only two of `a`, `b`, and `c` are provided.',
'This overrides `ternary<i>.sum` to normalize this specific',
'trace, but does not affect the values displayed on the axes.',
'0 (or missing) means to use ternary<i>.sum'
].join(' ')
},
mode: extendFlat({}, scatterAttrs.mode, {dflt: 'markers'}),
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (a,b,c) point.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of strings, the items are mapped in order to the',
'the data points in (a,b,c).'
].join(' ')
}),
line: {
color: scatterLineAttrs.color,
width: scatterLineAttrs.width,
dash: scatterLineAttrs.dash,
shape: extendFlat({}, scatterLineAttrs.shape,
{values: ['linear', 'spline']}),
smoothing: scatterLineAttrs.smoothing
},
connectgaps: scatterAttrs.connectgaps,
fill: extendFlat({}, scatterAttrs.fill, {
values: ['none', 'toself', 'tonext'],
description: [
'Sets the area to fill with a solid color.',
'Use with `fillcolor` if not *none*.',
'scatterternary has a subset of the options available to scatter.',
'*toself* connects the endpoints of the trace (or each segment',
'of the trace if it has gaps) into a closed shape.',
'*tonext* fills the space between two traces if one completely',
'encloses the other (eg consecutive contour lines), and behaves like',
'*toself* if there is no trace before it. *tonext* should not be',
'used if one trace does not enclose the other.'
].join(' ')
}),
fillcolor: scatterAttrs.fillcolor,
marker: extendFlat({}, {
symbol: scatterMarkerAttrs.symbol,
opacity: scatterMarkerAttrs.opacity,
maxdisplayed: scatterMarkerAttrs.maxdisplayed,
size: scatterMarkerAttrs.size,
sizeref: scatterMarkerAttrs.sizeref,
sizemin: scatterMarkerAttrs.sizemin,
sizemode: scatterMarkerAttrs.sizemode,
line: extendFlat({},
{width: scatterMarkerLineAttrs.width},
colorAttributes('marker'.line)
)
}, colorAttributes('marker'), {
showscale: scatterMarkerAttrs.showscale,
colorbar: colorbarAttrs
}),
textfont: scatterAttrs.textfont,
textposition: scatterAttrs.textposition,
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['a', 'b', 'c', 'text', 'name']
}),
hoveron: scatterAttrs.hoveron,
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterCarpet = {};
ScatterCarpet.attributes = require('./attributes');
ScatterCarpet.supplyDefaults = require('./defaults');
ScatterCarpet.colorbar = require('../scatter/colorbar');
ScatterCarpet.calc = require('./calc');
ScatterCarpet.plot = require('./plot');
ScatterCarpet.style = require('./style');
ScatterCarpet.hoverPoints = require('./hover');
ScatterCarpet.selectPoints = require('./select');
ScatterCarpet.moduleType = 'trace';
ScatterCarpet.name = 'scattercarpet';
ScatterCarpet.basePlotModule = require('../../plots/cartesian');
ScatterCarpet.categories = ['carpet', 'symbols', 'markerColorscale', 'showLegend', 'carpetDependent'];
ScatterCarpet.meta = {
hrName: 'scatter_carpet',
description: [
'Plots a scatter trace on either the first carpet axis or the',
'carpet axis with a matching `carpet` attribute.'
].join(' ')
};
module.exports = ScatterCarpet;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 14.29% | (1 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (1 / 7) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 6 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var dash = require('../../components/drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
var scatterMarkerAttrs = scatterAttrs.marker,
scatterLineAttrs = scatterAttrs.line,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
module.exports = {
lon: {
valType: 'data_array',
description: 'Sets the longitude coordinates (in degrees East).'
},
lat: {
valType: 'data_array',
description: 'Sets the latitude coordinates (in degrees North).'
},
locations: {
valType: 'data_array',
description: [
'Sets the coordinates via location IDs or names.',
'Coordinates correspond to the centroid of each location given.',
'See `locationmode` for more info.'
].join(' ')
},
locationmode: {
valType: 'enumerated',
values: ['ISO-3', 'USA-states', 'country names'],
role: 'info',
dflt: 'ISO-3',
description: [
'Determines the set of locations used to match entries in `locations`',
'to regions on the map.'
].join(' ')
},
mode: extendFlat({}, scatterAttrs.mode, {dflt: 'markers'}),
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (lon,lat) pair',
'or item in `locations`.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) or `locations` coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets hover text elements associated with each (lon,lat) pair',
'or item in `locations`.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) or `locations` coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
textfont: scatterAttrs.textfont,
textposition: scatterAttrs.textposition,
line: {
color: scatterLineAttrs.color,
width: scatterLineAttrs.width,
dash: dash
},
connectgaps: scatterAttrs.connectgaps,
marker: extendFlat({}, {
symbol: scatterMarkerAttrs.symbol,
opacity: scatterMarkerAttrs.opacity,
size: scatterMarkerAttrs.size,
sizeref: scatterMarkerAttrs.sizeref,
sizemin: scatterMarkerAttrs.sizemin,
sizemode: scatterMarkerAttrs.sizemode,
showscale: scatterMarkerAttrs.showscale,
colorbar: scatterMarkerAttrs.colorbar,
line: extendFlat({},
{width: scatterMarkerLineAttrs.width},
colorAttributes('marker.line')
)
},
colorAttributes('marker')
),
fill: {
valType: 'enumerated',
values: ['none', 'toself'],
dflt: 'none',
role: 'style',
description: [
'Sets the area to fill with a solid color.',
'Use with `fillcolor` if not *none*.',
'*toself* connects the endpoints of the trace (or each segment',
'of the trace if it has gaps) into a closed shape.'
].join(' ')
},
fillcolor: scatterAttrs.fillcolor,
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['lon', 'lat', 'location', 'text', 'name']
})
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterGeo = {};
ScatterGeo.attributes = require('./attributes');
ScatterGeo.supplyDefaults = require('./defaults');
ScatterGeo.colorbar = require('../scatter/colorbar');
ScatterGeo.calc = require('./calc');
ScatterGeo.plot = require('./plot');
ScatterGeo.hoverPoints = require('./hover');
ScatterGeo.eventData = require('./event_data');
ScatterGeo.moduleType = 'trace';
ScatterGeo.name = 'scattergeo';
ScatterGeo.basePlotModule = require('../../plots/geo');
ScatterGeo.categories = ['geo', 'symbols', 'markerColorscale', 'showLegend'];
ScatterGeo.meta = {
hrName: 'scatter_geo',
description: [
'The data visualized as scatter point or lines on a geographic map',
'is provided either by longitude/latitude pairs in `lon` and `lat`',
'respectively or by geographic location IDs or names in `locations`.'
].join(' ')
};
module.exports = ScatterGeo;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 12.5% | (1 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 12.5% | (1 / 8) | |
| index.js | 16.67% | (2 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 16.67% | (2 / 12) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | 4 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var DASHES = require('../../constants/gl2d_dashes');
var MARKERS = require('../../constants/gl_markers');
var extendFlat = require('../../lib/extend').extendFlat;
var extendDeep = require('../../lib/extend').extendDeep;
var scatterLineAttrs = scatterAttrs.line,
scatterMarkerAttrs = scatterAttrs.marker,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
module.exports = {
x: scatterAttrs.x,
x0: scatterAttrs.x0,
dx: scatterAttrs.dx,
y: scatterAttrs.y,
y0: scatterAttrs.y0,
dy: scatterAttrs.dy,
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (x,y) pair to appear on hover.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (x,y) coordinates.'
].join(' ')
}),
mode: {
valType: 'flaglist',
flags: ['lines', 'markers'],
extras: ['none'],
role: 'info',
description: [
'Determines the drawing mode for this scatter trace.'
].join(' ')
},
line: {
color: scatterLineAttrs.color,
width: scatterLineAttrs.width,
dash: {
valType: 'enumerated',
values: Object.keys(DASHES),
dflt: 'solid',
role: 'style',
description: 'Sets the style of the lines.'
}
},
marker: extendDeep({}, colorAttributes('marker'), {
symbol: {
valType: 'enumerated',
values: Object.keys(MARKERS),
dflt: 'circle',
arrayOk: true,
role: 'style',
description: 'Sets the marker symbol type.'
},
size: scatterMarkerAttrs.size,
sizeref: scatterMarkerAttrs.sizeref,
sizemin: scatterMarkerAttrs.sizemin,
sizemode: scatterMarkerAttrs.sizemode,
opacity: scatterMarkerAttrs.opacity,
showscale: scatterMarkerAttrs.showscale,
colorbar: scatterMarkerAttrs.colorbar,
line: extendDeep({}, colorAttributes('marker.line'), {
width: scatterMarkerLineAttrs.width
})
}),
connectgaps: scatterAttrs.connectgaps,
fill: extendFlat({}, scatterAttrs.fill, {
values: ['none', 'tozeroy', 'tozerox']
}),
fillcolor: scatterAttrs.fillcolor,
error_y: scatterAttrs.error_y,
error_x: scatterAttrs.error_x
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterGl = {};
ScatterGl.attributes = require('./attributes');
ScatterGl.supplyDefaults = require('./defaults');
ScatterGl.colorbar = require('../scatter/colorbar');
// reuse the Scatter3D 'dummy' calc step so that legends know what to do
ScatterGl.calc = require('../scatter3d/calc');
ScatterGl.plot = require('./convert');
ScatterGl.moduleType = 'trace';
ScatterGl.name = 'scattergl';
ScatterGl.basePlotModule = require('../../plots/gl2d');
ScatterGl.categories = ['gl2d', 'symbols', 'errorBarsOK', 'markerColorscale', 'showLegend'];
ScatterGl.meta = {
description: [
'The data visualized as scatter point or lines is set in `x` and `y`',
'using the WebGl plotting engine.',
'Bubble charts are achieved by setting `marker.size` and/or `marker.color`',
'to a numerical arrays.'
].join(' ')
};
module.exports = ScatterGl;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 11.11% | (1 / 9) | 100% | (0 / 0) | 100% | (0 / 0) | 11.11% | (1 / 9) | |
| index.js | 14.29% | (2 / 14) | 100% | (0 / 0) | 100% | (0 / 0) | 14.29% | (2 / 14) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterGeoAttrs = require('../scattergeo/attributes');
var scatterAttrs = require('../scatter/attributes');
var mapboxAttrs = require('../../plots/mapbox/layout_attributes');
var plotAttrs = require('../../plots/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
var lineAttrs = scatterGeoAttrs.line;
var markerAttrs = scatterGeoAttrs.marker;
module.exports = {
lon: scatterGeoAttrs.lon,
lat: scatterGeoAttrs.lat,
// locations
// locationmode
mode: extendFlat({}, scatterAttrs.mode, {
dflt: 'markers',
description: [
'Determines the drawing mode for this scatter trace.',
'If the provided `mode` includes *text* then the `text` elements',
'appear at the coordinates. Otherwise, the `text` elements',
'appear on hover.'
].join(' ')
}),
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (lon,lat) pair',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) coordinates.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets hover text elements associated with each (lon,lat) pair',
'If a single string, the same string appears over',
'all the data points.',
'If an array of string, the items are mapped in order to the',
'this trace\'s (lon,lat) coordinates.',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
line: {
color: lineAttrs.color,
width: lineAttrs.width
// TODO
// dash: dash
},
connectgaps: scatterAttrs.connectgaps,
marker: {
symbol: {
valType: 'string',
dflt: 'circle',
role: 'style',
arrayOk: true,
description: [
'Sets the marker symbol.',
'Full list: https://www.mapbox.com/maki-icons/',
'Note that the array `marker.color` and `marker.size`',
'are only available for *circle* symbols.'
].join(' ')
},
opacity: extendFlat({}, markerAttrs.opacity, {
arrayOk: false
}),
size: markerAttrs.size,
sizeref: markerAttrs.sizeref,
sizemin: markerAttrs.sizemin,
sizemode: markerAttrs.sizemode,
color: markerAttrs.color,
colorscale: markerAttrs.colorscale,
cauto: markerAttrs.cauto,
cmax: markerAttrs.cmax,
cmin: markerAttrs.cmin,
autocolorscale: markerAttrs.autocolorscale,
reversescale: markerAttrs.reversescale,
showscale: markerAttrs.showscale,
colorbar: colorbarAttrs
// line
},
fill: scatterGeoAttrs.fill,
fillcolor: scatterAttrs.fillcolor,
textfont: mapboxAttrs.layers.symbol.textfont,
textposition: mapboxAttrs.layers.symbol.textposition,
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['lon', 'lat', 'text', 'name']
}),
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterMapbox = {};
ScatterMapbox.attributes = require('./attributes');
ScatterMapbox.supplyDefaults = require('./defaults');
ScatterMapbox.colorbar = require('../scatter/colorbar');
ScatterMapbox.calc = require('../scattergeo/calc');
ScatterMapbox.hoverPoints = require('./hover');
ScatterMapbox.eventData = require('./event_data');
ScatterMapbox.plot = require('./plot');
ScatterMapbox.moduleType = 'trace';
ScatterMapbox.name = 'scattermapbox';
ScatterMapbox.basePlotModule = require('../../plots/mapbox');
ScatterMapbox.categories = ['mapbox', 'gl', 'symbols', 'markerColorscale', 'showLegend'];
ScatterMapbox.meta = {
hrName: 'scatter_mapbox',
description: [
'The data visualized as scatter point, lines or marker symbols',
'on a Mapbox GL geographic map',
'is provided by longitude/latitude pairs in `lon` and `lat`.'
].join(' ')
};
module.exports = ScatterMapbox;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 12.5% | (1 / 8) | 100% | (0 / 0) | 100% | (0 / 0) | 12.5% | (1 / 8) | |
| index.js | 13.33% | (2 / 15) | 100% | (0 / 0) | 100% | (0 / 0) | 13.33% | (2 / 15) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var scatterAttrs = require('../scatter/attributes');
var plotAttrs = require('../../plots/attributes');
var colorAttributes = require('../../components/colorscale/color_attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var dash = require('../../components/drawing/attributes').dash;
var extendFlat = require('../../lib/extend').extendFlat;
var scatterMarkerAttrs = scatterAttrs.marker,
scatterLineAttrs = scatterAttrs.line,
scatterMarkerLineAttrs = scatterMarkerAttrs.line;
module.exports = {
a: {
valType: 'data_array',
description: [
'Sets the quantity of component `a` in each data point.',
'If `a`, `b`, and `c` are all provided, they need not be',
'normalized, only the relative values matter. If only two',
'arrays are provided they must be normalized to match',
'`ternary<i>.sum`.'
].join(' ')
},
b: {
valType: 'data_array',
description: [
'Sets the quantity of component `a` in each data point.',
'If `a`, `b`, and `c` are all provided, they need not be',
'normalized, only the relative values matter. If only two',
'arrays are provided they must be normalized to match',
'`ternary<i>.sum`.'
].join(' ')
},
c: {
valType: 'data_array',
description: [
'Sets the quantity of component `a` in each data point.',
'If `a`, `b`, and `c` are all provided, they need not be',
'normalized, only the relative values matter. If only two',
'arrays are provided they must be normalized to match',
'`ternary<i>.sum`.'
].join(' ')
},
sum: {
valType: 'number',
role: 'info',
dflt: 0,
min: 0,
description: [
'The number each triplet should sum to,',
'if only two of `a`, `b`, and `c` are provided.',
'This overrides `ternary<i>.sum` to normalize this specific',
'trace, but does not affect the values displayed on the axes.',
'0 (or missing) means to use ternary<i>.sum'
].join(' ')
},
mode: extendFlat({}, scatterAttrs.mode, {dflt: 'markers'}),
text: extendFlat({}, scatterAttrs.text, {
description: [
'Sets text elements associated with each (a,b,c) point.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of strings, the items are mapped in order to the',
'the data points in (a,b,c).',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}),
hovertext: extendFlat({}, scatterAttrs.hovertext, {
description: [
'Sets hover text elements associated with each (a,b,c) point.',
'If a single string, the same string appears over',
'all the data points.',
'If an array of strings, the items are mapped in order to the',
'the data points in (a,b,c).',
'To be seen, trace `hoverinfo` must contain a *text* flag.'
].join(' ')
}),
line: {
color: scatterLineAttrs.color,
width: scatterLineAttrs.width,
dash: dash,
shape: extendFlat({}, scatterLineAttrs.shape,
{values: ['linear', 'spline']}),
smoothing: scatterLineAttrs.smoothing
},
connectgaps: scatterAttrs.connectgaps,
fill: extendFlat({}, scatterAttrs.fill, {
values: ['none', 'toself', 'tonext'],
description: [
'Sets the area to fill with a solid color.',
'Use with `fillcolor` if not *none*.',
'scatterternary has a subset of the options available to scatter.',
'*toself* connects the endpoints of the trace (or each segment',
'of the trace if it has gaps) into a closed shape.',
'*tonext* fills the space between two traces if one completely',
'encloses the other (eg consecutive contour lines), and behaves like',
'*toself* if there is no trace before it. *tonext* should not be',
'used if one trace does not enclose the other.'
].join(' ')
}),
fillcolor: scatterAttrs.fillcolor,
marker: extendFlat({}, {
symbol: scatterMarkerAttrs.symbol,
opacity: scatterMarkerAttrs.opacity,
maxdisplayed: scatterMarkerAttrs.maxdisplayed,
size: scatterMarkerAttrs.size,
sizeref: scatterMarkerAttrs.sizeref,
sizemin: scatterMarkerAttrs.sizemin,
sizemode: scatterMarkerAttrs.sizemode,
line: extendFlat({},
{width: scatterMarkerLineAttrs.width},
colorAttributes('marker'.line)
)
}, colorAttributes('marker'), {
showscale: scatterMarkerAttrs.showscale,
colorbar: colorbarAttrs
}),
textfont: scatterAttrs.textfont,
textposition: scatterAttrs.textposition,
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
flags: ['a', 'b', 'c', 'text', 'name']
}),
hoveron: scatterAttrs.hoveron,
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var ScatterTernary = {};
ScatterTernary.attributes = require('./attributes');
ScatterTernary.supplyDefaults = require('./defaults');
ScatterTernary.colorbar = require('../scatter/colorbar');
ScatterTernary.calc = require('./calc');
ScatterTernary.plot = require('./plot');
ScatterTernary.style = require('./style');
ScatterTernary.hoverPoints = require('./hover');
ScatterTernary.selectPoints = require('./select');
ScatterTernary.moduleType = 'trace';
ScatterTernary.name = 'scatterternary';
ScatterTernary.basePlotModule = require('../../plots/ternary');
ScatterTernary.categories = ['ternary', 'symbols', 'markerColorscale', 'showLegend'];
ScatterTernary.meta = {
hrName: 'scatter_ternary',
description: [
'Provides similar functionality to the *scatter* type but on a ternary phase diagram.',
'The data is provided by at least two arrays out of `a`, `b`, `c` triplets.'
].join(' ')
};
module.exports = ScatterTernary;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| attributes.js | 100% | (9 / 9) | 100% | (0 / 0) | 100% | (2 / 2) | 100% | (9 / 9) | |
| calc.js | 40% | (2 / 5) | 0% | (0 / 2) | 0% | (0 / 1) | 40% | (2 / 5) | |
| colorbar.js | 33.33% | (6 / 18) | 0% | (0 / 8) | 0% | (0 / 1) | 37.5% | (6 / 16) | |
| convert.js | 12.27% | (20 / 163) | 0% | (0 / 51) | 0% | (0 / 17) | 12.42% | (20 / 161) | |
| defaults.js | 12.96% | (7 / 54) | 0% | (0 / 20) | 0% | (0 / 4) | 12.96% | (7 / 54) | |
| index.js | 75% | (9 / 12) | 100% | (0 / 0) | 100% | (0 / 0) | 75% | (9 / 12) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 1 1 1 1 1 9 1 3 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Color = require('../../components/color');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var extendFlat = require('../../lib/extend').extendFlat;
function makeContourProjAttr(axLetter) {
return {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Determines whether or not these contour lines are projected',
'on the', axLetter, 'plane.',
'If `highlight` is set to *true* (the default), the projected',
'lines are shown on hover.',
'If `show` is set to *true*, the projected lines are shown',
'in permanence.'
].join(' ')
};
}
function makeContourAttr(axLetter) {
return {
show: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Determines whether or not contour lines about the', axLetter,
'dimension are drawn.'
].join(' ')
},
project: {
x: makeContourProjAttr('x'),
y: makeContourProjAttr('y'),
z: makeContourProjAttr('z')
},
color: {
valType: 'color',
role: 'style',
dflt: Color.defaultLine,
description: 'Sets the color of the contour lines.'
},
usecolormap: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'An alternate to *color*.',
'Determines whether or not the contour lines are colored using',
'the trace *colorscale*.'
].join(' ')
},
width: {
valType: 'number',
role: 'style',
min: 1,
max: 16,
dflt: 2,
description: 'Sets the width of the contour lines.'
},
highlight: {
valType: 'boolean',
role: 'info',
dflt: true,
description: [
'Determines whether or not contour lines about the', axLetter,
'dimension are highlighted on hover.'
].join(' ')
},
highlightcolor: {
valType: 'color',
role: 'style',
dflt: Color.defaultLine,
description: 'Sets the color of the highlighted contour lines.'
},
highlightwidth: {
valType: 'number',
role: 'style',
min: 1,
max: 16,
dflt: 2,
description: 'Sets the width of the highlighted contour lines.'
}
};
}
module.exports = {
z: {
valType: 'data_array',
description: 'Sets the z coordinates.'
},
x: {
valType: 'data_array',
description: 'Sets the x coordinates.'
},
y: {
valType: 'data_array',
description: 'Sets the y coordinates.'
},
text: {
valType: 'data_array',
description: 'Sets the text elements associated with each z value.'
},
surfacecolor: {
valType: 'data_array',
description: [
'Sets the surface color values,',
'used for setting a color scale independent of `z`.'
].join(' ')
},
// Todo this block has a structure of colorscale/attributes.js but with colorscale/color_attributes.js names
cauto: colorscaleAttrs.zauto,
cmin: colorscaleAttrs.zmin,
cmax: colorscaleAttrs.zmax,
colorscale: colorscaleAttrs.colorscale,
autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale,
{dflt: false}),
reversescale: colorscaleAttrs.reversescale,
showscale: colorscaleAttrs.showscale,
colorbar: colorbarAttrs,
contours: {
x: makeContourAttr('x'),
y: makeContourAttr('y'),
z: makeContourAttr('z')
},
hidesurface: {
valType: 'boolean',
role: 'info',
dflt: false,
description: [
'Determines whether or not a surface is drawn.',
'For example, set `hidesurface` to *false*',
'`contours.x.show` to *true* and',
'`contours.y.show` to *true* to draw a wire frame plot.'
].join(' ')
},
lightposition: {
x: {
valType: 'number',
role: 'style',
min: -1e5,
max: 1e5,
dflt: 10,
description: 'Numeric vector, representing the X coordinate for each vertex.'
},
y: {
valType: 'number',
role: 'style',
min: -1e5,
max: 1e5,
dflt: 1e4,
description: 'Numeric vector, representing the Y coordinate for each vertex.'
},
z: {
valType: 'number',
role: 'style',
min: -1e5,
max: 1e5,
dflt: 0,
description: 'Numeric vector, representing the Z coordinate for each vertex.'
}
},
lighting: {
ambient: {
valType: 'number',
role: 'style',
min: 0.00,
max: 1.0,
dflt: 0.8,
description: 'Ambient light increases overall color visibility but can wash out the image.'
},
diffuse: {
valType: 'number',
role: 'style',
min: 0.00,
max: 1.00,
dflt: 0.8,
description: 'Represents the extent that incident rays are reflected in a range of angles.'
},
specular: {
valType: 'number',
role: 'style',
min: 0.00,
max: 2.00,
dflt: 0.05,
description: 'Represents the level that incident rays are reflected in a single direction, causing shine.'
},
roughness: {
valType: 'number',
role: 'style',
min: 0.00,
max: 1.00,
dflt: 0.5,
description: 'Alters specular reflection; the rougher the surface, the wider and less contrasty the shine.'
},
fresnel: {
valType: 'number',
role: 'style',
min: 0.00,
max: 5.00,
dflt: 0.2,
description: [
'Represents the reflectance as a dependency of the viewing angle; e.g. paper is reflective',
'when viewing it from the edge of the paper (almost 90 degrees), causing shine.'
].join(' ')
}
},
opacity: {
valType: 'number',
role: 'style',
min: 0,
max: 1,
dflt: 1,
description: 'Sets the opacity of the surface.'
},
_deprecated: {
zauto: extendFlat({}, colorscaleAttrs.zauto, {
description: 'Obsolete. Use `cauto` instead.'
}),
zmin: extendFlat({}, colorscaleAttrs.zmin, {
description: 'Obsolete. Use `cmin` instead.'
}),
zmax: extendFlat({}, colorscaleAttrs.zmax, {
description: 'Obsolete. Use `cmax` instead.'
})
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var colorscaleCalc = require('../../components/colorscale/calc');
// Compute auto-z and autocolorscale if applicable
module.exports = function calc(gd, trace) {
if(trace.surfacecolor) {
colorscaleCalc(trace, trace.surfacecolor, '', 'c');
} else {
colorscaleCalc(trace, trace.z, '', 'c');
}
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var Plots = require('../../plots/plots');
var Colorscale = require('../../components/colorscale');
var drawColorbar = require('../../components/colorbar/draw');
module.exports = function colorbar(gd, cd) {
var trace = cd[0].trace,
cbId = 'cb' + trace.uid,
cmin = trace.cmin,
cmax = trace.cmax,
vals = trace.surfacecolor || trace.z;
if(!isNumeric(cmin)) cmin = Lib.aggNums(Math.min, null, vals);
if(!isNumeric(cmax)) cmax = Lib.aggNums(Math.max, null, vals);
gd._fullLayout._infolayer.selectAll('.' + cbId).remove();
if(!trace.showscale) {
Plots.autoMargin(gd, cbId);
return;
}
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
var sclFunc = Colorscale.makeColorScaleFunc(
Colorscale.extractScale(
trace.colorscale,
cmin,
cmax
),
{ noNumericCheck: true }
);
cb.fillcolor(sclFunc)
.filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254})
.options(trace.colorbar)();
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var createSurface = require('gl-surface3d');
var ndarray = require('ndarray');
var homography = require('ndarray-homography');
var fill = require('ndarray-fill');
var ops = require('ndarray-ops');
var tinycolor = require('tinycolor2');
var str2RgbaArray = require('../../lib/str2rgbarray');
var MIN_RESOLUTION = 128;
function SurfaceTrace(scene, surface, uid) {
this.scene = scene;
this.uid = uid;
this.surface = surface;
this.data = null;
this.showContour = [false, false, false];
this.dataScale = 1.0;
}
var proto = SurfaceTrace.prototype;
proto.handlePick = function(selection) {
if(selection.object === this.surface) {
var selectIndex = [
Math.min(
Math.round(selection.data.index[0] / this.dataScale - 1)|0,
this.data.z[0].length - 1
),
Math.min(
Math.round(selection.data.index[1] / this.dataScale - 1)|0,
this.data.z.length - 1
)
];
var traceCoordinate = [0, 0, 0];
if(Array.isArray(this.data.x[0])) {
traceCoordinate[0] = this.data.x[selectIndex[1]][selectIndex[0]];
} else {
traceCoordinate[0] = this.data.x[selectIndex[0]];
}
if(Array.isArray(this.data.y[0])) {
traceCoordinate[1] = this.data.y[selectIndex[1]][selectIndex[0]];
} else {
traceCoordinate[1] = this.data.y[selectIndex[1]];
}
traceCoordinate[2] = this.data.z[selectIndex[1]][selectIndex[0]];
selection.traceCoordinate = traceCoordinate;
var sceneLayout = this.scene.fullSceneLayout;
selection.dataCoordinate = [
sceneLayout.xaxis.d2l(traceCoordinate[0], 0, this.data.xcalendar) * this.scene.dataScale[0],
sceneLayout.yaxis.d2l(traceCoordinate[1], 0, this.data.ycalendar) * this.scene.dataScale[1],
sceneLayout.zaxis.d2l(traceCoordinate[2], 0, this.data.zcalendar) * this.scene.dataScale[2]
];
var text = this.data.text;
if(text && text[selectIndex[1]] && text[selectIndex[1]][selectIndex[0]] !== undefined) {
selection.textLabel = text[selectIndex[1]][selectIndex[0]];
}
else selection.textLabel = '';
selection.data.dataCoordinate = selection.dataCoordinate.slice();
this.surface.highlight(selection.data);
// Snap spikes to data coordinate
this.scene.glplot.spikes.position = selection.dataCoordinate;
return true;
}
};
function parseColorScale(colorscale, alpha) {
if(alpha === undefined) alpha = 1;
return colorscale.map(function(elem) {
var index = elem[0];
var color = tinycolor(elem[1]);
var rgb = color.toRgb();
return {
index: index,
rgb: [rgb.r, rgb.g, rgb.b, alpha]
};
});
}
function isColormapCircular(colormap) {
var first = colormap[0].rgb,
last = colormap[colormap.length - 1].rgb;
return (
first[0] === last[0] &&
first[1] === last[1] &&
first[2] === last[2] &&
first[3] === last[3]
);
}
// Pad coords by +1
function padField(field) {
var shape = field.shape;
var nshape = [shape[0] + 2, shape[1] + 2];
var nfield = ndarray(new Float32Array(nshape[0] * nshape[1]), nshape);
// Center
ops.assign(nfield.lo(1, 1).hi(shape[0], shape[1]), field);
// Edges
ops.assign(nfield.lo(1).hi(shape[0], 1),
field.hi(shape[0], 1));
ops.assign(nfield.lo(1, nshape[1] - 1).hi(shape[0], 1),
field.lo(0, shape[1] - 1).hi(shape[0], 1));
ops.assign(nfield.lo(0, 1).hi(1, shape[1]),
field.hi(1));
ops.assign(nfield.lo(nshape[0] - 1, 1).hi(1, shape[1]),
field.lo(shape[0] - 1));
// Corners
nfield.set(0, 0, field.get(0, 0));
nfield.set(0, nshape[1] - 1, field.get(0, shape[1] - 1));
nfield.set(nshape[0] - 1, 0, field.get(shape[0] - 1, 0));
nfield.set(nshape[0] - 1, nshape[1] - 1, field.get(shape[0] - 1, shape[1] - 1));
return nfield;
}
function refine(coords) {
var minScale = Math.max(coords[0].shape[0], coords[0].shape[1]);
if(minScale < MIN_RESOLUTION) {
var scaleF = MIN_RESOLUTION / minScale;
var nshape = [
Math.floor((coords[0].shape[0]) * scaleF + 1)|0,
Math.floor((coords[0].shape[1]) * scaleF + 1)|0 ];
var nsize = nshape[0] * nshape[1];
for(var i = 0; i < coords.length; ++i) {
var padImg = padField(coords[i]);
var scaledImg = ndarray(new Float32Array(nsize), nshape);
homography(scaledImg, padImg, [scaleF, 0, 0,
0, scaleF, 0,
0, 0, 1]);
coords[i] = scaledImg;
}
return scaleF;
}
return 1.0;
}
proto.setContourLevels = function() {
var nlevels = [[], [], []];
var needsUpdate = false;
for(var i = 0; i < 3; ++i) {
if(this.showContour[i]) {
needsUpdate = true;
nlevels[i] = this.scene.contourLevels[i];
}
}
if(needsUpdate) {
this.surface.update({ levels: nlevels });
}
};
proto.update = function(data) {
var i,
scene = this.scene,
sceneLayout = scene.fullSceneLayout,
surface = this.surface,
alpha = data.opacity,
colormap = parseColorScale(data.colorscale, alpha),
z = data.z,
x = data.x,
y = data.y,
xaxis = sceneLayout.xaxis,
yaxis = sceneLayout.yaxis,
zaxis = sceneLayout.zaxis,
scaleFactor = scene.dataScale,
xlen = z[0].length,
ylen = z.length,
coords = [
ndarray(new Float32Array(xlen * ylen), [xlen, ylen]),
ndarray(new Float32Array(xlen * ylen), [xlen, ylen]),
ndarray(new Float32Array(xlen * ylen), [xlen, ylen])
],
xc = coords[0],
yc = coords[1],
contourLevels = scene.contourLevels;
// Save data
this.data = data;
/*
* Fill and transpose zdata.
* Consistent with 'heatmap' and 'contour', plotly 'surface'
* 'z' are such that sub-arrays correspond to y-coords
* and that the sub-array entries correspond to a x-coords,
* which is the transpose of 'gl-surface-plot'.
*/
var xcalendar = data.xcalendar,
ycalendar = data.ycalendar,
zcalendar = data.zcalendar;
fill(coords[2], function(row, col) {
return zaxis.d2l(z[col][row], 0, zcalendar) * scaleFactor[2];
});
// coords x
if(Array.isArray(x[0])) {
fill(xc, function(row, col) {
return xaxis.d2l(x[col][row], 0, xcalendar) * scaleFactor[0];
});
} else {
// ticks x
fill(xc, function(row) {
return xaxis.d2l(x[row], 0, xcalendar) * scaleFactor[0];
});
}
// coords y
if(Array.isArray(y[0])) {
fill(yc, function(row, col) {
return yaxis.d2l(y[col][row], 0, ycalendar) * scaleFactor[1];
});
} else {
// ticks y
fill(yc, function(row, col) {
return yaxis.d2l(y[col], 0, ycalendar) * scaleFactor[1];
});
}
var params = {
colormap: colormap,
levels: [[], [], []],
showContour: [true, true, true],
showSurface: !data.hidesurface,
contourProject: [
[false, false, false],
[false, false, false],
[false, false, false]
],
contourWidth: [1, 1, 1],
contourColor: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
contourTint: [1, 1, 1],
dynamicColor: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
dynamicWidth: [1, 1, 1],
dynamicTint: [1, 1, 1],
opacity: data.opacity
};
params.intensityBounds = [data.cmin, data.cmax];
// Refine if necessary
if(data.surfacecolor) {
var intensity = ndarray(new Float32Array(xlen * ylen), [xlen, ylen]);
fill(intensity, function(row, col) {
return data.surfacecolor[col][row];
});
coords.push(intensity);
}
else {
// when 'z' is used as 'intensity',
// we must scale its value
params.intensityBounds[0] *= scaleFactor[2];
params.intensityBounds[1] *= scaleFactor[2];
}
this.dataScale = refine(coords);
if(data.surfacecolor) {
params.intensity = coords.pop();
}
var highlightEnable = [true, true, true];
var axis = ['x', 'y', 'z'];
for(i = 0; i < 3; ++i) {
var contourParams = data.contours[axis[i]];
highlightEnable[i] = contourParams.highlight;
params.showContour[i] = contourParams.show || contourParams.highlight;
if(!params.showContour[i]) continue;
params.contourProject[i] = [
contourParams.project.x,
contourParams.project.y,
contourParams.project.z
];
if(contourParams.show) {
this.showContour[i] = true;
params.levels[i] = contourLevels[i];
surface.highlightColor[i] = params.contourColor[i] = str2RgbaArray(contourParams.color);
if(contourParams.usecolormap) {
surface.highlightTint[i] = params.contourTint[i] = 0;
}
else {
surface.highlightTint[i] = params.contourTint[i] = 1;
}
params.contourWidth[i] = contourParams.width;
} else {
this.showContour[i] = false;
}
if(contourParams.highlight) {
params.dynamicColor[i] = str2RgbaArray(contourParams.highlightcolor);
params.dynamicWidth[i] = contourParams.highlightwidth;
}
}
// see https://github.com/plotly/plotly.js/issues/940
if(isColormapCircular(colormap)) {
params.vertexColor = true;
}
params.coords = coords;
surface.update(params);
surface.visible = data.visible;
surface.enableDynamic = highlightEnable;
surface.snapToData = true;
if('lighting' in data) {
surface.ambientLight = data.lighting.ambient;
surface.diffuseLight = data.lighting.diffuse;
surface.specularLight = data.lighting.specular;
surface.roughness = data.lighting.roughness;
surface.fresnel = data.lighting.fresnel;
}
if('lightposition' in data) {
surface.lightPosition = [data.lightposition.x, data.lightposition.y, data.lightposition.z];
}
if(alpha && alpha < 1) {
surface.supportsTransparency = true;
}
};
proto.dispose = function() {
this.scene.glplot.remove(this.surface);
this.surface.dispose();
};
function createSurfaceTrace(scene, data) {
var gl = scene.glplot.gl;
var surface = createSurface({ gl: gl });
var result = new SurfaceTrace(scene, surface, data.uid);
surface._trace = result;
result.update(data);
scene.glplot.add(surface);
return result;
}
module.exports = createSurfaceTrace;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | 1 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var colorscaleDefaults = require('../../components/colorscale/defaults');
var attributes = require('./attributes');
module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout) {
var i, j;
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
}
var z = coerce('z');
if(!z) {
traceOut.visible = false;
return;
}
var xlen = z[0].length;
var ylen = z.length;
coerce('x');
coerce('y');
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);
if(!Array.isArray(traceOut.x)) {
// build a linearly scaled x
traceOut.x = [];
for(i = 0; i < xlen; ++i) {
traceOut.x[i] = i;
}
}
coerce('text');
if(!Array.isArray(traceOut.y)) {
traceOut.y = [];
for(i = 0; i < ylen; ++i) {
traceOut.y[i] = i;
}
}
// Coerce remaining properties
[
'lighting.ambient',
'lighting.diffuse',
'lighting.specular',
'lighting.roughness',
'lighting.fresnel',
'lightposition.x',
'lightposition.y',
'lightposition.z',
'hidesurface',
'opacity'
].forEach(function(x) { coerce(x); });
var surfaceColor = coerce('surfacecolor');
coerce('colorscale');
var dims = ['x', 'y', 'z'];
for(i = 0; i < 3; ++i) {
var contourDim = 'contours.' + dims[i];
var show = coerce(contourDim + '.show');
var highlight = coerce(contourDim + '.highlight');
if(show || highlight) {
for(j = 0; j < 3; ++j) {
coerce(contourDim + '.project.' + dims[j]);
}
}
if(show) {
coerce(contourDim + '.color');
coerce(contourDim + '.width');
coerce(contourDim + '.usecolormap');
}
if(highlight) {
coerce(contourDim + '.highlightcolor');
coerce(contourDim + '.highlightwidth');
}
}
// backward compatibility block
if(!surfaceColor) {
mapLegacy(traceIn, 'zmin', 'cmin');
mapLegacy(traceIn, 'zmax', 'cmax');
mapLegacy(traceIn, 'zauto', 'cauto');
}
// TODO if contours.?.usecolormap are false and hidesurface is true
// the colorbar shouldn't be shown by default
colorscaleDefaults(
traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'}
);
};
function mapLegacy(traceIn, oldAttr, newAttr) {
if(oldAttr in traceIn && !(newAttr in traceIn)) {
traceIn[newAttr] = traceIn[oldAttr];
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 2 2 2 2 2 2 2 2 2 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Surface = {};
Surface.attributes = require('./attributes');
Surface.supplyDefaults = require('./defaults');
Surface.colorbar = require('./colorbar');
Surface.calc = require('./calc');
Surface.plot = require('./convert');
Surface.moduleType = 'trace';
Surface.name = 'surface';
Surface.basePlotModule = require('../../plots/gl3d');
Surface.categories = ['gl3d', 'noOpacity'];
Surface.meta = {
description: [
'The data the describes the coordinates of the surface is set in `z`.',
'Data in `z` should be a {2D array}.',
'Coordinates in `x` and `y` can either be 1D {arrays}',
'or {2D arrays} (e.g. to graph parametric surfaces).',
'If not provided in `x` and `y`, the x and y coordinates are assumed',
'to be linear starting at 0 with a unit step.',
'The color scale corresponds to the `z` values by default.',
'For custom color scales, use `surfacecolor` which should be a {2D array},',
'where its bounds can be controlled using `cmin` and `cmax`.'
].join(' ')
};
module.exports = Surface;
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| filter.js | 7.09% | (9 / 127) | 0% | (0 / 76) | 0% | (0 / 27) | 8.04% | (9 / 112) | |
| groupby.js | 13.95% | (6 / 43) | 0% | (0 / 12) | 0% | (0 / 6) | 14.63% | (6 / 41) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | 4 4 4 1 1 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var Registry = require('../registry');
var PlotSchema = require('../plot_api/plot_schema');
var axisIds = require('../plots/cartesian/axis_ids');
var autoType = require('../plots/cartesian/axis_autotype');
var setConvert = require('../plots/cartesian/set_convert');
var INEQUALITY_OPS = ['=', '<', '>=', '>', '<='];
var INTERVAL_OPS = ['[]', '()', '[)', '(]', '][', ')(', '](', ')['];
var SET_OPS = ['{}', '}{'];
exports.moduleType = 'transform';
exports.name = 'filter';
exports.attributes = {
enabled: {
valType: 'boolean',
dflt: true,
description: [
'Determines whether this filter transform is enabled or disabled.'
].join(' ')
},
target: {
valType: 'string',
strict: true,
noBlank: true,
arrayOk: true,
dflt: 'x',
description: [
'Sets the filter target by which the filter is applied.',
'If a string, *target* is assumed to be a reference to a data array',
'in the parent trace object.',
'To filter about nested variables, use *.* to access them.',
'For example, set `target` to *marker.color* to filter',
'about the marker color array.',
'If an array, *target* is then the data array by which the filter is applied.'
].join(' ')
},
operation: {
valType: 'enumerated',
values: [].concat(INEQUALITY_OPS).concat(INTERVAL_OPS).concat(SET_OPS),
dflt: '=',
description: [
'Sets the filter operation.',
'*=* keeps items equal to `value`',
'*<* keeps items less than `value`',
'*<=* keeps items less than or equal to `value`',
'*>* keeps items greater than `value`',
'*>=* keeps items greater than or equal to `value`',
'*[]* keeps items inside `value[0]` to value[1]` including both bounds`',
'*()* keeps items inside `value[0]` to value[1]` excluding both bounds`',
'*[)* keeps items inside `value[0]` to value[1]` including `value[0]` but excluding `value[1]',
'*(]* keeps items inside `value[0]` to value[1]` excluding `value[0]` but including `value[1]',
'*][* keeps items outside `value[0]` to value[1]` and equal to both bounds`',
'*)(* keeps items outside `value[0]` to value[1]`',
'*](* keeps items outside `value[0]` to value[1]` and equal to `value[0]`',
'*)[* keeps items outside `value[0]` to value[1]` and equal to `value[1]`',
'*{}* keeps items present in a set of values',
'*}{* keeps items not present in a set of values'
].join(' ')
},
value: {
valType: 'any',
dflt: 0,
description: [
'Sets the value or values by which to filter by.',
'Values are expected to be in the same type as the data linked',
'to *target*.',
'When `operation` is set to one of the inequality values',
'(' + INEQUALITY_OPS + ')',
'*value* is expected to be a number or a string.',
'When `operation` is set to one of the interval value',
'(' + INTERVAL_OPS + ')',
'*value* is expected to be 2-item array where the first item',
'is the lower bound and the second item is the upper bound.',
'When `operation`, is set to one of the set value',
'(' + SET_OPS + ')',
'*value* is expected to be an array with as many items as',
'the desired set elements.'
].join(' ')
}
};
exports.supplyDefaults = function(transformIn) {
var transformOut = {};
function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}
var enabled = coerce('enabled');
if(enabled) {
coerce('operation');
coerce('value');
coerce('target');
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(transformIn, transformOut, 'valuecalendar', null);
handleCalendarDefaults(transformIn, transformOut, 'targetcalendar', null);
}
return transformOut;
};
exports.calcTransform = function(gd, trace, opts) {
if(!opts.enabled) return;
var target = opts.target,
filterArray = getFilterArray(trace, target),
len = filterArray.length;
if(!len) return;
var targetCalendar = opts.targetcalendar;
// even if you provide targetcalendar, if target is a string and there
// is a calendar attribute matching target it will get used instead.
if(typeof target === 'string') {
var attrTargetCalendar = Lib.nestedProperty(trace, target + 'calendar').get();
if(attrTargetCalendar) targetCalendar = attrTargetCalendar;
}
// if target points to an axis, use the type we already have for that
// axis to find the data type. Otherwise use the values to autotype.
var d2cTarget = (target === 'x' || target === 'y' || target === 'z') ?
target : filterArray;
var dataToCoord = getDataToCoordFunc(gd, trace, d2cTarget),
filterFunc = getFilterFunc(opts, dataToCoord, targetCalendar),
arrayAttrs = PlotSchema.findArrayAttributes(trace),
originalArrays = {};
// copy all original array attribute values,
// and clear arrays in trace
for(var k = 0; k < arrayAttrs.length; k++) {
var attr = arrayAttrs[k],
np = Lib.nestedProperty(trace, attr);
originalArrays[attr] = Lib.extendDeep([], np.get());
np.set([]);
}
function fill(attr, i) {
var oldArr = originalArrays[attr],
newArr = Lib.nestedProperty(trace, attr).get();
newArr.push(oldArr[i]);
}
for(var i = 0; i < len; i++) {
var v = filterArray[i];
if(!filterFunc(v)) continue;
for(var j = 0; j < arrayAttrs.length; j++) {
fill(arrayAttrs[j], i);
}
}
};
function getFilterArray(trace, target) {
if(typeof target === 'string' && target) {
var array = Lib.nestedProperty(trace, target).get();
return Array.isArray(array) ? array : [];
}
else if(Array.isArray(target)) return target.slice();
return false;
}
function getDataToCoordFunc(gd, trace, target) {
var ax;
// In the case of an array target, make a mock data array
// and call supplyDefaults to the data type and
// setup the data-to-calc method.
if(Array.isArray(target)) {
ax = {
type: autoType(target),
_categories: []
};
setConvert(ax);
if(ax.type === 'category') {
// build up ax._categories (usually done during ax.makeCalcdata()
for(var i = 0; i < target.length; i++) {
ax.d2c(target[i]);
}
}
}
else {
ax = axisIds.getFromTrace(gd, trace, target);
}
// if 'target' has corresponding axis
// -> use setConvert method
if(ax) return ax.d2c;
// special case for 'ids'
// -> cast to String
if(target === 'ids') return function(v) { return String(v); };
// otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
// -> cast to Number
return function(v) { return +v; };
}
function getFilterFunc(opts, d2c, targetCalendar) {
var operation = opts.operation,
value = opts.value,
hasArrayValue = Array.isArray(value);
function isOperationIn(array) {
return array.indexOf(operation) !== -1;
}
var d2cValue = function(v) { return d2c(v, 0, opts.valuecalendar); },
d2cTarget = function(v) { return d2c(v, 0, targetCalendar); };
var coercedValue;
if(isOperationIn(INEQUALITY_OPS)) {
coercedValue = hasArrayValue ? d2cValue(value[0]) : d2cValue(value);
}
else if(isOperationIn(INTERVAL_OPS)) {
coercedValue = hasArrayValue ?
[d2cValue(value[0]), d2cValue(value[1])] :
[d2cValue(value), d2cValue(value)];
}
else if(isOperationIn(SET_OPS)) {
coercedValue = hasArrayValue ? value.map(d2cValue) : [d2cValue(value)];
}
switch(operation) {
case '=':
return function(v) { return d2cTarget(v) === coercedValue; };
case '<':
return function(v) { return d2cTarget(v) < coercedValue; };
case '<=':
return function(v) { return d2cTarget(v) <= coercedValue; };
case '>':
return function(v) { return d2cTarget(v) > coercedValue; };
case '>=':
return function(v) { return d2cTarget(v) >= coercedValue; };
case '[]':
return function(v) {
var cv = d2cTarget(v);
return cv >= coercedValue[0] && cv <= coercedValue[1];
};
case '()':
return function(v) {
var cv = d2cTarget(v);
return cv > coercedValue[0] && cv < coercedValue[1];
};
case '[)':
return function(v) {
var cv = d2cTarget(v);
return cv >= coercedValue[0] && cv < coercedValue[1];
};
case '(]':
return function(v) {
var cv = d2cTarget(v);
return cv > coercedValue[0] && cv <= coercedValue[1];
};
case '][':
return function(v) {
var cv = d2cTarget(v);
return cv <= coercedValue[0] || cv >= coercedValue[1];
};
case ')(':
return function(v) {
var cv = d2cTarget(v);
return cv < coercedValue[0] || cv > coercedValue[1];
};
case '](':
return function(v) {
var cv = d2cTarget(v);
return cv <= coercedValue[0] || cv > coercedValue[1];
};
case ')[':
return function(v) {
var cv = d2cTarget(v);
return cv < coercedValue[0] || cv >= coercedValue[1];
};
case '{}':
return function(v) {
return coercedValue.indexOf(d2cTarget(v)) !== -1;
};
case '}{':
return function(v) {
return coercedValue.indexOf(d2cTarget(v)) === -1;
};
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 4 4 1 1 1 1 | /**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../lib');
var PlotSchema = require('../plot_api/plot_schema');
exports.moduleType = 'transform';
exports.name = 'groupby';
exports.attributes = {
enabled: {
valType: 'boolean',
dflt: true,
description: [
'Determines whether this group-by transform is enabled or disabled.'
].join(' ')
},
groups: {
valType: 'data_array',
dflt: [],
description: [
'Sets the groups in which the trace data will be split.',
'For example, with `x` set to *[1, 2, 3, 4]* and',
'`groups` set to *[\'a\', \'b\', \'a\', \'b\']*,',
'the groupby transform with split in one trace',
'with `x` [1, 3] and one trace with `x` [2, 4].'
].join(' ')
},
style: {
valType: 'any',
dflt: {},
description: [
'Sets each group style.',
'For example, with `groups` set to *[\'a\', \'b\', \'a\', \'b\']*',
'and `style` set to *{ a: { marker: { color: \'red\' } }}',
'marker points in group *\'a\'* will be drawn in red.'
].join(' ')
}
};
/**
* Supply transform attributes defaults
*
* @param {object} transformIn
* object linked to trace.transforms[i] with 'type' set to exports.name
* @param {object} fullData
* the plot's full data
* @param {object} layout
* the plot's (not-so-full) layout
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.supplyDefaults = function(transformIn) {
var transformOut = {};
function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}
var enabled = coerce('enabled');
if(!enabled) return transformOut;
coerce('groups');
coerce('style');
return transformOut;
};
/**
* Apply transform !!!
*
* @param {array} data
* array of transformed traces (is [fullTrace] upon first transform)
*
* @param {object} state
* state object which includes:
* - transform {object} full transform attributes
* - fullTrace {object} full trace object which is being transformed
* - fullData {array} full pre-transform(s) data array
* - layout {object} the plot's (not-so-full) layout
*
* @return {object} newData
* array of transformed traces
*/
exports.transform = function(data, state) {
var newData = [];
for(var i = 0; i < data.length; i++) {
newData = newData.concat(transformOne(data[i], state));
}
return newData;
};
function initializeArray(newTrace, a) {
Lib.nestedProperty(newTrace, a).set([]);
}
function pasteArray(newTrace, trace, j, a) {
Lib.nestedProperty(newTrace, a).set(
Lib.nestedProperty(newTrace, a).get().concat([
Lib.nestedProperty(trace, a).get()[j]
])
);
}
function transformOne(trace, state) {
var opts = state.transform;
var groups = trace.transforms[state.transformIndex].groups;
if(!(Array.isArray(groups)) || groups.length === 0) {
return trace;
}
var groupNames = Lib.filterUnique(groups),
newData = new Array(groupNames.length),
len = groups.length;
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
var style = opts.style || {};
for(var i = 0; i < groupNames.length; i++) {
var groupName = groupNames[i];
var newTrace = newData[i] = Lib.extendDeepNoArrays({}, trace);
arrayAttrs.forEach(initializeArray.bind(null, newTrace));
for(var j = 0; j < len; j++) {
if(groups[j] !== groupName) continue;
arrayAttrs.forEach(pasteArray.bind(0, newTrace, trace, j));
}
newTrace.name = groupName;
// there's no need to coerce style[groupName] here
// as another round of supplyDefaults is done on the transformed traces
newTrace = Lib.extendDeepNoArrays(newTrace, style[groupName] || {});
}
return newData;
}
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| baseline.js | 57.14% | (4 / 7) | 100% | (0 / 0) | 100% | (0 / 0) | 57.14% | (4 / 7) | |
| bundle.js | 23.08% | (3 / 13) | 0% | (0 / 6) | 0% | (0 / 1) | 23.08% | (3 / 13) | |
| cibundle.js | 50% | (2 / 4) | 100% | (0 / 0) | 100% | (0 / 0) | 50% | (2 / 4) | |
| docker.js | 40% | (12 / 30) | 12.5% | (1 / 8) | 0% | (0 / 1) | 41.38% | (12 / 29) | |
| header.js | 17.39% | (8 / 46) | 0% | (0 / 8) | 0% | (0 / 11) | 17.78% | (8 / 45) | |
| preprocess.js | 16.67% | (4 / 24) | 0% | (0 / 4) | 0% | (0 / 5) | 18.18% | (4 / 22) | |
| pretest.js | 92% | (23 / 25) | 50% | (1 / 2) | 100% | (5 / 5) | 92% | (23 / 25) | |
| stats.js | 24.29% | (17 / 70) | 0% | (0 / 18) | 0% | (0 / 14) | 25.37% | (17 / 67) | |
| watch.js | 25% | (1 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 25% | (1 / 4) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 2 2 2 2 | var constants = require('./util/constants');
var common = require('./util/common');
var containerCommands = require('./util/container_commands');
var msg = [
'Generating baseline image(s) using build/plotly.js from',
common.getTimeLastModified(constants.pathToPlotlyBuild),
'\n'
].join(' ');
var cmd = containerCommands.getRunCmd(
process.env.CIRCLECI,
'node test/image/make_baseline.js ' + process.argv.slice(2).join(' ')
);
console.log(msg);
common.execCmd(cmd);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 2 2 2 | var constants = require('./util/constants');
var common = require('./util/common');
var _bundle = require('./util/browserify_wrapper');
/*
* This script takes one argument
*
* Run `npm run build -- dev` or `npm run build -- --dev`
* to include source map in the plotly.js bundle
*
* N.B. This script is meant for dist builds; the output bundles are placed
* in plotly.js/dist/.
* Use `npm run watch` for dev builds.
*/
var arg = process.argv[2];
var DEV = (arg === 'dev') || (arg === '--dev');
// Check if style and font build files are there
var doesFileExist = common.doesFileExist;
if(!doesFileExist(constants.pathToCSSBuild) || !doesFileExist(constants.pathToFontSVG)) {
throw new Error([
'build/ is missing one or more files',
'Please run `npm run preprocess` first'
].join('\n'));
}
// Browserify plotly.js
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDist, {
standalone: 'Plotly',
debug: DEV,
pathToMinBundle: constants.pathToPlotlyDistMin
});
// Browserify the geo assets
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
standalone: 'PlotlyGeoAssets'
});
// Browserify the plotly.js with meta
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyDistWithMeta, {
standalone: 'Plotly',
debug: DEV
});
// Browserify the plotly.js partial bundles
constants.partialBundlePaths.forEach(function(pathObj) {
_bundle(pathObj.index, pathObj.dist, {
standalone: 'Plotly',
debug: DEV,
pathToMinBundle: pathObj.distMin
});
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 2 2 | var constants = require('./util/constants');
var _bundle = require('./util/browserify_wrapper');
/*
* Trimmed down version of ./bundle.js for CI testing
*
* Outputs:
*
* - plotly.js bundle in build/
* - plotly-geo-assets.js bundle in dist/ (in accordance with test/image/index.html)
* - plotly.min.js bundle in dist/ (for requirejs test)
*/
// Browserify plotly.js and plotly.min.js
_bundle(constants.pathToPlotlyIndex, constants.pathToPlotlyBuild, {
standalone: 'Plotly',
pathToMinBundle: constants.pathToPlotlyDistMin,
});
// Browserify the geo assets
_bundle(constants.pathToPlotlyGeoAssetsSrc, constants.pathToPlotlyGeoAssetsDist, {
standalone: 'PlotlyGeoAssets'
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 1 1 1 1 1 1 1 1 1 1 1 1 | var constants = require('./util/constants');
var common = require('./util/common');
var containerCommands = require('./util/container_commands');
var isCI = process.env.CIRCLECI;
var arg = process.argv[2];
var msg, cmd, cb, errorCb;
switch(arg) {
case 'pull':
msg = 'Pulling latest docker image';
cmd = 'docker pull ' + constants.testContainerImage;
break;
case 'run':
msg = 'Booting up ' + constants.testContainerName + ' docker container';
cmd = containerCommands.dockerRun;
// if docker-run fails, try docker-start.
errorCb = function(err) {
if(err) common.execCmd('docker start ' + constants.testContainerName);
};
break;
case 'setup':
msg = 'Setting up ' + constants.testContainerName + ' docker container for testing';
cmd = containerCommands.getRunCmd(isCI, containerCommands.setup);
break;
case 'stop':
msg = 'Stopping ' + constants.testContainerName + ' docker container';
cmd = 'docker stop ' + constants.testContainerName;
break;
case 'remove':
msg = 'Removing ' + constants.testContainerName + ' docker container';
cmd = 'docker rm ' + constants.testContainerName;
break;
default:
console.log('Usage: pull, run, setup, stop, remove');
process.exit(0);
break;
}
console.log(msg);
common.execCmd(cmd, cb, errorCb);
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 2 2 2 1 1 1 1 1 | var path = require('path');
var fs = require('fs');
var prependFile = require('prepend-file');
var falafel = require('falafel');
var glob = require('glob');
var constants = require('./util/constants');
var common = require('./util/common');
// main
addHeadersInDistFiles();
updateHeadersInSrcFiles();
// add headers to dist files
function addHeadersInDistFiles() {
function _prepend(path, header) {
prependFile(path, header + '\n', common.throwOnError);
}
// add header to main dist bundles
var pathsDist = [
constants.pathToPlotlyDistMin,
constants.pathToPlotlyDist,
constants.pathToPlotlyDistWithMeta,
constants.pathToPlotlyGeoAssetsDist
];
pathsDist.forEach(function(path) {
_prepend(path, constants.licenseDist);
});
// add header and bundle name to partial bundle
constants.partialBundlePaths.forEach(function(pathObj) {
var headerDist = constants.licenseDist
.replace('plotly.js', 'plotly.js (' + pathObj.name + ')');
_prepend(pathObj.dist, headerDist);
var headerDistMin = constants.licenseDist
.replace('plotly.js', 'plotly.js (' + pathObj.name + ' - minified)');
_prepend(pathObj.distMin, headerDistMin);
});
}
// add or update header to src/ lib/ files
function updateHeadersInSrcFiles() {
var srcGlob = path.join(constants.pathToSrc, '**/*.js');
var libGlob = path.join(constants.pathToLib, '**/*.js');
// remove leading '/*' and trailing '*/' for comparison with falafel output
var licenseSrc = constants.licenseSrc;
var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);
glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) {
files.forEach(function(file) {
fs.readFile(file, 'utf-8', function(err, code) {
// parse through code string while keeping track of comments
var comments = [];
falafel(code, {onComment: comments, locations: true}, function() {});
var header = comments[0];
// error out if no header is found
if(!header || header.loc.start.line > 1) {
throw new Error(file + ' : has no header information.');
}
// if header and license are the same, do nothing
if(isCorrect(header)) return;
// if header and license only differ by date, update header
else if(hasWrongDate(header)) {
var codeLines = code.split('\n');
codeLines.splice(header.loc.start.line - 1, header.loc.end.line);
var newCode = licenseSrc + '\n' + codeLines.join('\n');
common.writeFile(file, newCode);
}
else {
// otherwise, throw an error
throw new Error(file + ' : has wrong header information.');
}
});
});
});
function isCorrect(header) {
return (header.value === licenseStr);
}
function hasWrongDate(header) {
var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;
return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
}
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 2 1 1 1 | var fs = require('fs-extra');
var sass = require('node-sass');
var constants = require('./util/constants');
var common = require('./util/common');
var pullCSS = require('./util/pull_css');
var pullFontSVG = require('./util/pull_font_svg');
var updateVersion = require('./util/update_version');
// main
makeBuildCSS();
makeBuildFontSVG();
copyTopojsonFiles();
updateVersion(constants.pathToPlotlyCore);
updateVersion(constants.pathToPlotlyGeoAssetsSrc);
// convert scss to css to js
function makeBuildCSS() {
sass.render({
file: constants.pathToSCSS,
outputStyle: 'compressed'
}, function(err, result) {
if(err) throw err;
// css to js
pullCSS(String(result.css), constants.pathToCSSBuild);
});
}
// convert font svg into js
function makeBuildFontSVG() {
fs.readFile(constants.pathToFontSVG, function(err, data) {
if(err) throw err;
pullFontSVG(data.toString(), constants.pathToFontSVGBuild);
});
}
// copy topojson files from sane-topojson to dist/
function copyTopojsonFiles() {
fs.copy(
constants.pathToTopojsonSrc,
constants.pathToTopojsonDist,
{ clobber: true },
common.throwOnError
);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 1 1 1 1 1 1 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1 4 | var fs = require('fs');
var constants = require('./util/constants');
var common = require('./util/common');
// main
makeCredentialsFile();
makeTestImageFolders();
makeRequireJSFixture();
// Create a credentials json file,
// to be required in jasmine test suites and test dashboard
function makeCredentialsFile() {
var credentials = JSON.stringify({
MAPBOX_ACCESS_TOKEN: constants.mapboxAccessToken
}, null, 2);
common.writeFile(constants.pathToCredentials, credentials);
logger('make build/credentials.json');
}
// Make artifact folders for image tests
function makeTestImageFolders() {
function makeOne(folderPath, info) {
Iif(!common.doesDirExist(folderPath)) {
fs.mkdirSync(folderPath);
logger('initialize ' + info);
}
else logger(info + ' is present');
}
makeOne(constants.pathToTestImages, 'test image folder');
makeOne(constants.pathToTestImagesDiff, 'test image diff folder');
}
// Make script file that define plotly in a RequireJS context
function makeRequireJSFixture() {
var bundle = fs.readFileSync(constants.pathToPlotlyDistMin, 'utf-8');
var index = [
'define(\'plotly\', function(require, exports, module) {',
bundle,
'});'
].join('');
common.writeFile(constants.pathToRequireJSFixture, index);
logger('make build/requirejs_fixture.js');
}
function logger(task) {
console.log('ok ' + task);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 | var path = require('path');
var fs = require('fs');
var spawn = require('child_process').spawn;
var falafel = require('falafel');
var gzipSize = require('gzip-size');
var prettySize = require('prettysize');
var common = require('./util/common');
var constants = require('./util/constants');
var pkg = require('../package.json');
var pathDistREADME = path.join(constants.pathToDist, 'README.md');
var pathDistNpmLs = path.join(constants.pathToDist, 'npm-ls.json');
var cdnRoot = 'https://cdn.plot.ly/plotly-';
var coreModules = ['scatter'];
var ENC = 'utf-8';
var JS = '.js';
var MINJS = '.min.js';
// main
writeNpmLs();
common.writeFile(pathDistREADME, getReadMeContent());
function writeNpmLs() {
if(common.doesFileExist(pathDistNpmLs)) fs.unlinkSync(pathDistNpmLs);
var ws = fs.createWriteStream(pathDistNpmLs, { flags: 'a' });
var proc = spawn('npm', ['ls', '--json', '--only', 'prod']);
proc.stdout.pipe(ws);
}
function getReadMeContent() {
return []
.concat(getInfoContent())
.concat(getMainBundleInfo())
.concat(getPartialBundleInfo())
.concat(getFooter())
.join('\n');
}
// general info about distributed files
function getInfoContent() {
return [
'# Using distributed files',
'',
'All plotly.js dist bundles inject an object `Plotly` into the global scope.',
'',
'Import plotly.js as:',
'',
'```html',
'<script type="text/javascript" src="plotly.min.js"></script>',
'```',
'',
'or the un-minified version as:',
'',
'```html',
'<script type="text/javascript" src="plotly.js" charset="utf-8"></script>',
'```',
'',
'To support IE9, put:',
'',
'```html',
'<script>if(typeof window.Int16Array !== \'function\')document.write("<scri"+"pt src=\'extras/typedarray.min.js\'></scr"+"ipt>");</script>',
'<script>document.write("<scri"+"pt src=\'extras/request_animation_frame.js\'></scr"+"ipt>");</script>',
'```',
'',
'before the plotly.js script tag.',
'',
'To add MathJax, put',
'',
'```html',
'<script type="text/javascript" src="mathjax/MathJax.js?config=TeX-AMS-MML_SVG"></script>',
'```',
'',
'before the plotly.js script tag. You can grab the relevant MathJax files in `./dist/extras/mathjax/`.',
''
];
}
// info about main bundle
function getMainBundleInfo() {
var mainSizes = findSizes({
dist: constants.pathToPlotlyDist,
distMin: constants.pathToPlotlyDistMin,
withMeta: constants.pathToPlotlyDistWithMeta
});
return [
'# Bundle information',
'',
'The main plotly.js bundle includes all the official (non-beta) trace modules.',
'',
'It be can imported as minified javascript',
'- using dist file `dist/plotly.min.js`',
'- using CDN URL ' + cdnRoot + 'latest' + MINJS + ' OR ' + cdnRoot + pkg.version + MINJS,
'',
'or as raw javascript:',
'- using dist file `dist/plotly.js`',
'- using CDN URL ' + cdnRoot + 'latest' + JS + ' OR ' + cdnRoot + pkg.version + JS,
'- using CommonJS with `require(\'plotly.js\')`',
'',
'If you would like to have access to the attribute meta information ' +
'(including attribute descriptions as on the [schema reference page](https://plot.ly/javascript/reference/)), ' +
'use dist file `dist/plotly-with-meta.js`',
'',
'The main plotly.js bundle weights in at:',
'',
'| plotly.js | plotly.min.js | plotly.min.js + gzip | plotly-with-meta.js |',
'|-----------|---------------|----------------------|---------------------|',
'| ' + mainSizes.raw + ' | ' + mainSizes.minified + ' | ' + mainSizes.gzipped + ' | ' + mainSizes.withMeta + ' |',
'',
'## Partial bundles',
'',
'Starting in `v1.15.0`, plotly.js also ships with several _partial_ bundles:',
'',
constants.partialBundlePaths.map(makeBundleHeaderInfo).join('\n'),
''
];
}
// info about partial bundles
function getPartialBundleInfo() {
return constants.partialBundlePaths.map(makeBundleInfo);
}
// footer info
function getFooter() {
return [
'----------------',
'',
'_This file is auto-generated by `npm run stats`. ' +
'Please do not edit this file directly._'
];
}
function makeBundleHeaderInfo(pathObj) {
var name = pathObj.name;
return '- [' + name + '](#plotlyjs-' + name + ')';
}
function makeBundleInfo(pathObj) {
var name = pathObj.name;
var sizes = findSizes(pathObj);
var moduleList = coreModules.concat(scrapeContent(pathObj));
return [
'### plotly.js ' + name,
'',
formatBundleInfo(name, moduleList),
'',
'| Way to import | Location |',
'|---------------|----------|',
'| dist bundle | ' + '`dist/plotly-' + name + JS + '` |',
'| dist bundle (minified) | ' + '`dist/plotly-' + name + MINJS + '` |',
'| CDN URL (latest) | ' + cdnRoot + name + '-latest' + JS + ' |',
'| CDN URL (latest minified) | ' + cdnRoot + name + '-latest' + MINJS + ' |',
'| CDN URL (tagged) | ' + cdnRoot + name + '-' + pkg.version + JS + ' |',
'| CDN URL (tagged minified) | ' + cdnRoot + name + '-' + pkg.version + MINJS + ' |',
'| CommonJS | ' + '`require(\'plotly.js/lib/' + 'index-' + name + '\')`' + ' |',
'',
'| Raw size | Minified size | Minified + gzip size |',
'|------|-----------------|------------------------|',
'| ' + sizes.raw + ' | ' + sizes.minified + ' | ' + sizes.gzipped + ' |',
''
].join('\n');
}
function findSizes(pathObj) {
var codeDist = fs.readFileSync(pathObj.dist, ENC),
codeDistMin = fs.readFileSync(pathObj.distMin, ENC);
var sizes = {
raw: prettySize(codeDist.length),
minified: prettySize(codeDistMin.length),
gzipped: prettySize(gzipSize.sync(codeDistMin))
};
if(pathObj.withMeta) {
var codeWithMeta = fs.readFileSync(pathObj.withMeta, ENC);
sizes.withMeta = prettySize(codeWithMeta.length);
}
return sizes;
}
function scrapeContent(pathObj) {
var code = fs.readFileSync(pathObj.index, ENC);
var moduleList = [];
falafel(code, function(node) {
if(isModuleNode(node)) {
var moduleName = node.value.replace('./', '');
moduleList.push(moduleName);
}
});
return moduleList;
}
function isModuleNode(node) {
return (
node.type === 'Literal' &&
node.parent &&
node.parent.type === 'CallExpression' &&
node.parent.callee &&
node.parent.callee.type === 'Identifier' &&
node.parent.callee.name === 'require' &&
node.parent.parent &&
node.parent.parent.type === 'ArrayExpression'
);
}
function formatBundleInfo(bundleName, moduleList) {
var enumeration = moduleList.map(function(moduleName, i) {
var len = moduleList.length,
ending;
if(i === len - 2) ending = ' and';
else if(i < len - 1) ending = ',';
else ending = '';
return '`' + moduleName + '`' + ending;
});
return [
'The', '`' + bundleName + '`',
'partial bundle contains the',
enumeration.join(' '),
'trace modules.'
].join(' ');
}
|
| 1 2 3 4 5 6 7 8 | 2 | var makeWatchifiedBundle = require('./util/watchified_bundle');
var noop = function() {};
// make a watchified bundle for plotly.js and run it!
var watchifiedBundle = makeWatchifiedBundle(noop);
watchifiedBundle();
|
| File | Statements | Branches | Functions | Lines | |||||
|---|---|---|---|---|---|---|---|---|---|
| browserify_wrapper.js | 13.33% | (4 / 30) | 0% | (0 / 12) | 0% | (0 / 5) | 14.29% | (4 / 28) | |
| common.js | 67.5% | (27 / 40) | 38.89% | (7 / 18) | 75% | (9 / 12) | 75.76% | (25 / 33) | |
| compress_attributes.js | 76.92% | (10 / 13) | 100% | (0 / 0) | 60% | (3 / 5) | 76.92% | (10 / 13) | |
| constants.js | 100% | (14 / 14) | 100% | (2 / 2) | 100% | (1 / 1) | 100% | (14 / 14) | |
| container_commands.js | 45% | (9 / 20) | 0% | (0 / 4) | 0% | (0 / 3) | 47.37% | (9 / 19) | |
| patch_minified.js | 75% | (3 / 4) | 100% | (0 / 0) | 0% | (0 / 1) | 75% | (3 / 4) | |
| pull_css.js | 10% | (2 / 20) | 0% | (0 / 10) | 0% | (0 / 4) | 11.76% | (2 / 17) | |
| pull_font_svg.js | 13.33% | (2 / 15) | 0% | (0 / 6) | 0% | (0 / 4) | 15.38% | (2 / 13) | |
| shortcut_paths.js | 13.33% | (2 / 15) | 0% | (0 / 4) | 0% | (0 / 2) | 14.29% | (2 / 14) | |
| update_version.js | 38.46% | (5 / 13) | 0% | (0 / 11) | 0% | (0 / 5) | 45.45% | (5 / 11) | |
| watchified_bundle.js | 12.5% | (4 / 32) | 0% | (0 / 6) | 0% | (0 / 7) | 12.9% | (4 / 31) |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | 6 6 6 1 | var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var UglifyJS = require('uglify-js');
var constants = require('./constants');
var compressAttributes = require('./compress_attributes');
var patchMinified = require('./patch_minified');
/** Convenience browserify wrapper
*
* @param {string} pathToIndex path to index file to bundle
* @param {string} pathToBunlde path to destination bundle
*
* @param {object} opts
*
* Browserify options:
* - standalone {string}
* - debug {boolean} [optional]
*
* Additional option:
* - pathToMinBundle {string} path to destination minified bundle
*
* Outputs one bundle (un-minified) file if opts.pathToMinBundle is omitted
* or opts.debug is true. Otherwise outputs two file: one un-minified bundle and
* one minified bundle.
*
* Logs basename of bundle when completed.
*/
module.exports = function _bundle(pathToIndex, pathToBundle, opts) {
opts = opts || {};
// do we output a minified file?
var pathToMinBundle = opts.pathToMinBundle,
outputMinified = !!pathToMinBundle && !opts.debug;
var browserifyOpts = {};
browserifyOpts.standalone = opts.standalone;
browserifyOpts.debug = opts.debug;
browserifyOpts.transform = outputMinified ? [compressAttributes] : [];
var b = browserify(pathToIndex, browserifyOpts),
bundleWriteStream = fs.createWriteStream(pathToBundle);
bundleWriteStream.on('finish', function() {
logger(pathToBundle);
});
b.bundle(function(err, buf) {
if(err) throw err;
if(outputMinified) {
var minifiedCode = UglifyJS.minify(buf.toString(), constants.uglifyOptions).code;
minifiedCode = patchMinified(minifiedCode);
fs.writeFile(pathToMinBundle, minifiedCode, function(err) {
if(err) throw err;
logger(pathToMinBundle);
});
}
})
.pipe(bundleWriteStream);
};
function logger(pathToOutput) {
var log = 'ok ' + path.basename(pathToOutput);
console.log(log);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 1 2 2 2 1 1 2 2 1 1 | var fs = require('fs');
var exec = require('child_process').exec;
exports.execCmd = function(cmd, cb, errorCb) {
cb = cb ? cb : function() {};
errorCb = errorCb ? errorCb : function(err) { Iif(err) throw err; };
exec(cmd, function(err) {
errorCb(err);
cb();
})
.stdout.pipe(process.stdout);
};
exports.writeFile = function(filePath, content, cb) {
fs.writeFile(filePath, content, function(err) {
Iif(err) throw err;
Iif(cb) cb();
});
};
exports.doesDirExist = function(dirPath) {
try {
Eif(fs.statSync(dirPath).isDirectory()) return true;
}
catch(e) {
return false;
}
return false;
};
exports.doesFileExist = function(filePath) {
try {
if(fs.statSync(filePath).isFile()) return true;
}
catch(e) {
return false;
}
return false;
};
exports.formatTime = function(date) {
return [
date.toLocaleDateString(),
date.toLocaleTimeString(),
date.toString().match(/\(([A-Za-z\s].*)\)/)[1]
].join(' ');
};
exports.getTimeLastModified = function(filePath) {
Eif(!exports.doesFileExist(filePath)) {
throw new Error(filePath + ' does not exist');
}
var stats = fs.statSync(filePath),
formattedTime = exports.formatTime(stats.mtime);
return formattedTime;
};
exports.touch = function(filePath) {
fs.closeSync(fs.openSync(filePath, 'w'));
};
exports.throwOnError = function(err) {
if(err) throw err;
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 1 1 3 1 1 1 2 1 1 1 | var through = require('through2');
/**
* Browserify transform that strips meta attributes out
* of the plotly.js bundles
*/
// one line string with or without trailing comma
function makeStringRegex(attr) {
return attr + ': \'.*\'' + ',?';
}
// joined array of strings with or without trailing comma
function makeJoinedArrayRegex(attr) {
return attr + ': \\[[\\s\\S]*?\\]' + '\\.join\\(.*' + ',?';
}
// array with or without trailing comma
function makeArrayRegex(attr) {
return attr + ': \\[[\\s\\S]*?\\]' + ',?';
}
// ref: http://www.regexr.com/3cmac
var regexStr = [
makeStringRegex('description'),
makeJoinedArrayRegex('description'),
makeArrayRegex('requiredOpts'),
makeArrayRegex('otherOpts'),
makeStringRegex('hrName'),
makeStringRegex('role')
].join('|');
var regex = new RegExp(regexStr, 'g');
module.exports = function() {
return through(function(buf, enc, next) {
this.push(
buf.toString('utf-8')
.replace(regex, '')
);
next();
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 1 1 1 1 1 1 1 1 1 1 1 7 1 1 | var path = require('path');
var pkg = require('../../package.json');
var pathToRoot = path.join(__dirname, '../../');
var pathToSrc = path.join(pathToRoot, 'src/');
var pathToLib = path.join(pathToRoot, 'lib/');
var pathToImageTest = path.join(pathToRoot, 'test/image');
var pathToDist = path.join(pathToRoot, 'dist/');
var pathToBuild = path.join(pathToRoot, 'build/');
var pathToTopojsonSrc = path.join(
path.dirname(require.resolve('sane-topojson')), 'dist/'
);
var partialBundleNames = [
'basic', 'cartesian', 'geo', 'gl3d', 'gl2d', 'mapbox', 'finance'
];
var partialBundlePaths = partialBundleNames.map(function(name) {
return {
name: name,
index: path.join(pathToLib, 'index-' + name + '.js'),
dist: path.join(pathToDist, 'plotly-' + name + '.js'),
distMin: path.join(pathToDist, 'plotly-' + name + '.min.js')
};
});
var year = (new Date()).getFullYear();
module.exports = {
pathToRoot: pathToRoot,
pathToSrc: pathToSrc,
pathToLib: pathToLib,
pathToBuild: pathToBuild,
pathToDist: pathToDist,
pathToPlotlyIndex: path.join(pathToLib, 'index.js'),
pathToPlotlyCore: path.join(pathToSrc, 'core.js'),
pathToPlotlyBuild: path.join(pathToBuild, 'plotly.js'),
pathToPlotlyDist: path.join(pathToDist, 'plotly.js'),
pathToPlotlyDistMin: path.join(pathToDist, 'plotly.min.js'),
pathToPlotlyDistWithMeta: path.join(pathToDist, 'plotly-with-meta.js'),
partialBundleNames: partialBundleNames,
partialBundlePaths: partialBundlePaths,
pathToTopojsonSrc: pathToTopojsonSrc,
pathToTopojsonDist: path.join(pathToDist, 'topojson/'),
pathToPlotlyGeoAssetsSrc: path.join(pathToSrc, 'assets/geo_assets.js'),
pathToPlotlyGeoAssetsDist: path.join(pathToDist, 'plotly-geo-assets.js'),
pathToFontSVG: path.join(pathToSrc, 'fonts/ploticon/ploticon.svg'),
pathToFontSVGBuild: path.join(pathToBuild, 'ploticon.js'),
pathToSCSS: path.join(pathToSrc, 'css/style.scss'),
pathToCSSBuild: path.join(pathToBuild, 'plotcss.js'),
pathToTestDashboardBundle: path.join(pathToBuild, 'test_dashboard-bundle.js'),
pathToImageViewerBundle: path.join(pathToBuild, 'image_viewer-bundle.js'),
pathToTestImageMocks: path.join(pathToImageTest, 'mocks/'),
pathToTestImageBaselines: path.join(pathToImageTest, 'baselines/'),
pathToTestImages: path.join(pathToBuild, 'test_images/'),
pathToTestImagesDiff: path.join(pathToBuild, 'test_images_diff/'),
pathToTestImagesDiffList: path.join(pathToBuild, 'list_of_incorrect_images.txt'),
pathToJasmineTests: path.join(pathToRoot, 'test/jasmine/tests'),
pathToJasmineBundleTests: path.join(pathToRoot, 'test/jasmine/bundle_tests'),
pathToRequireJS: path.join(pathToRoot, 'node_modules', 'requirejs', 'require.js'),
pathToRequireJSFixture: path.join(pathToBuild, 'requirejs_fixture.js'),
// this mapbox access token is 'public', no need to hide it
// more info: https://www.mapbox.com/help/define-access-token/
mapboxAccessToken: 'pk.eyJ1IjoiZXRwaW5hcmQiLCJhIjoiY2luMHIzdHE0MGFxNXVubTRxczZ2YmUxaCJ9.hwWZful0U2CQxit4ItNsiQ',
pathToCredentials: path.join(pathToBuild, 'credentials.json'),
testContainerImage: 'plotly/testbed:latest',
testContainerName: process.env.PLOTLYJS_TEST_CONTAINER_NAME || 'imagetest',
testContainerPort: '9010',
testContainerUrl: 'http://localhost:9010/',
testContainerHome: '/var/www/streambed/image_server/plotly.js',
uglifyOptions: {
fromString: true,
mangle: true,
compress: {
warnings: false,
screw_ie8: true
},
output: {
beautify: false,
ascii_only: true
}
},
licenseDist: [
'/**',
'* plotly.js v' + pkg.version,
'* Copyright 2012-' + year + ', Plotly, Inc.',
'* All rights reserved.',
'* Licensed under the MIT license',
'*/'
].join('\n'),
licenseSrc: [
'/**',
'* Copyright 2012-' + year + ', Plotly, Inc.',
'* All rights reserved.',
'*',
'* This source code is licensed under the MIT license found in the',
'* LICENSE file in the root directory of this source tree.',
'*/'
].join('\n')
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 1 1 1 1 1 1 1 1 1 | var constants = require('./constants');
var containerCommands = {
cdHome: 'cd ' + constants.testContainerHome,
cpIndex: 'cp -f test/image/index.html ../server_app/index.html',
injectEnv: [
'sed -i',
's/process.env.PLOTLY_MAPBOX_DEFAULT_ACCESS_TOKEN/\\\'' + constants.mapboxAccessToken + '\\\'/',
'../server_app/main.js'
].join(' '),
restart: 'supervisorctl restart nw1'
};
containerCommands.ping = [
'wget',
'--server-response --spider --tries=20 --retry-connrefused',
constants.testContainerUrl + 'ping'
].join(' ');
containerCommands.setup = [
containerCommands.cpIndex,
containerCommands.injectEnv,
containerCommands.restart,
'sleep 1',
].join(' && ');
containerCommands.dockerRun = [
'docker run -d',
'--name', constants.testContainerName,
'-v', constants.pathToRoot + ':' + constants.testContainerHome,
'-p', constants.testContainerPort + ':' + constants.testContainerPort,
constants.testContainerImage
].join(' ');
containerCommands.getRunCmd = function(isCI, commands) {
var _commands = Array.isArray(commands) ? commands.slice() : [commands];
if(isCI) return getRunCI(_commands);
// add setup commands locally
_commands = [containerCommands.setup].concat(_commands);
return getRunLocal(_commands);
};
function getRunLocal(commands) {
commands = [containerCommands.cdHome].concat(commands);
var commandsJoined = '"' + commands.join(' && ') + '"';
return [
'docker exec -i',
constants.testContainerName,
'/bin/bash -c',
commandsJoined
].join(' ');
}
function getRunCI(commands) {
commands = ['export CIRCLECI=1', containerCommands.cdHome].concat(commands);
var commandsJoined = '"' + commands.join(' && ') + '"';
return [
'sudo',
'lxc-attach -n',
'$(docker inspect --format \'{{.Id}}\' ' + constants.testContainerName + ')',
'-- bash -c',
commandsJoined
].join(' ');
}
module.exports = containerCommands;
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 1 1 1 | var PATTERN = /require\("\+(\w)\((\w)\)\+"\)/;
var NEW_SUBSTR = 'require("+ $1($2) +")';
/* Uber hacky in-house fix to
*
* https://github.com/substack/webworkify/issues/29
*
* so that plotly.min.js loads in Jupyter NBs, more info here:
*
* - https://github.com/plotly/plotly.py/pull/545
* - https://github.com/plotly/plotly.js/pull/914
* - https://github.com/plotly/plotly.js/pull/1094
*
* For example, this routine replaces
* 'require("+o(s)+")' -> 'require("+ o(s) +")'
*
* But works for any 1-letter variable that uglify-js may output.
*
*/
module.exports = function patchMinified(minifiedCode) {
return minifiedCode.replace(PATTERN, NEW_SUBSTR);
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | 1 1 | var fs = require('fs');
module.exports = function pullCSS(data, pathOut) {
var rules = {};
data.split(/\s*\}\s*/).forEach(function(chunk) {
if(!chunk) return;
var parts = chunk.split(/\s*\{\s*/),
selectorList = parts[0],
rule = parts[1];
// take off ".js-plotly-plot .plotly", which should be on every selector
selectorList.split(/,\s*/).forEach(function(selector) {
if(!selector.match(/^([\.]js-plotly-plot [\.]plotly|[\.]plotly-notifier)/)) {
throw new Error('all plotlyjs-style selectors must start ' +
'.js-plotly-plot .plotly or .plotly-notifier\n' +
'found: ' + selectorList);
}
});
selectorList = selectorList
.replace(/[\.]js-plotly-plot [\.]plotly/g, 'X')
.replace(/[\.]plotly-notifier/g, 'Y');
// take out newlines in rule, and make sure it ends in a semicolon
rule = rule.replace(/;\s*/g, ';').replace(/;?\s*$/, ';');
// omit blank rules (why do we get these occasionally?)
if(rule.match(/^[\s;]*$/)) return;
rules[selectorList] = rules[selectorList] || '' + rule;
});
var rulesStr = JSON.stringify(rules, null, 4).replace(/\"(\w+)\":/g, '$1:');
var outStr = [
'\'use strict\';',
'',
'var Lib = require(\'../src/lib\');',
'var rules = ' + rulesStr + ';',
'',
'for(var selector in rules) {',
' var fullSelector = selector.replace(/^,/,\' ,\')',
' .replace(/X/g, \'.js-plotly-plot .plotly\')',
' .replace(/Y/g, \'.plotly-notifier\');',
' Lib.addStyleRule(fullSelector, rules[selector]);',
'}',
''
].join('\n');
fs.writeFile(pathOut, outStr, function(err) {
if(err) throw err;
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 2 2 | var fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
module.exports = function pullFontSVG(data, pathOut) {
parser.parseString(data, function(err, result) {
if(err) throw err;
var font_obj = result.svg.defs[0].font[0],
default_width = Number(font_obj.$['horiz-adv-x']),
ascent = Number(font_obj['font-face'][0].$.ascent),
descent = Number(font_obj['font-face'][0].$.descent),
chars = {};
font_obj.glyph.forEach(function(glyph) {
chars[glyph.$['glyph-name']] = {
width: Number(glyph.$['horiz-adv-x']) || default_width,
path: glyph.$.d,
ascent: ascent,
descent: descent
};
});
// turn remaining double quotes into single
var charStr = JSON.stringify(chars, null, 4).replace(/\"/g, '\'');
var outStr = [
'\'use strict\';',
'',
'module.exports = ' + charStr + ';',
''
].join('\n');
fs.writeFile(pathOut, outStr, function(err) {
if(err) throw err;
});
});
};
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 2 2 | var path = require('path');
var transformTools = require('browserify-transform-tools');
var constants = require('./constants');
/**
* Transform require paths starting with '@' to appropriate folder paths
*/
var shortcutsConfig = {
'@src': constants.pathToSrc,
'@lib': constants.pathToLib,
'@mocks': constants.pathToTestImageMocks,
'@build': constants.pathToBuild
};
module.exports = transformTools.makeRequireTransform('requireTransform',
{ evaluateArguments: true, jsFilesOnly: true },
function(args, opts, cb) {
var pathIn = args[0];
var pathOut;
Object.keys(shortcutsConfig).forEach(function(k) {
if(pathIn.indexOf(k) !== -1) {
var tail = pathIn.split(k)[1];
var newPath = path.join(shortcutsConfig[k], tail).replace(/\\/g, '/');
pathOut = 'require(\'' + newPath + '\')';
}
});
if(pathOut) return cb(null, pathOut);
else return cb();
});
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 1 1 1 1 1 | var fs = require('fs');
var falafel = require('falafel');
var pkg = require('../../package.json');
module.exports = function updateVersion(pathToFile) {
fs.readFile(pathToFile, 'utf-8', function(err, code) {
var out = falafel(code, function(node) {
if(isVersionNode(node)) node.update('\'' + pkg.version + '\'');
});
fs.writeFile(pathToFile, out, function(err) {
if(err) throw err;
});
});
};
function isVersionNode(node) {
return (
node.type === 'Literal' &&
node.parent &&
node.parent.type === 'AssignmentExpression' &&
node.parent.left &&
node.parent.left.object &&
node.parent.left.property &&
node.parent.left.property.name === 'version'
);
}
|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 4 4 1 1 | var fs = require('fs');
var browserify = require('browserify');
var watchify = require('watchify');
var prettySize = require('prettysize');
var constants = require('./constants');
var common = require('./common');
var compressAttributes = require('./compress_attributes');
/**
* Make a plotly.js browserify bundle function watched by watchify.
*
* N.B. This module is meant for dev builds; the output bundle is placed
* in plotly.js/build/
* Use `npm run build` for dist builds.
*
* @param {function} onFirstBundleCallback executed when first bundle is completed
*
*/
module.exports = function makeWatchifiedBundle(onFirstBundleCallback) {
var b = browserify(constants.pathToPlotlyIndex, {
debug: true,
standalone: 'Plotly',
transform: [compressAttributes],
cache: {},
packageCache: {},
plugin: [watchify]
});
var firstBundle = true;
if(firstBundle) {
console.log([
'***',
'Building the first bundle, this should take ~10 seconds',
'***\n'
].join(' '));
}
b.on('update', bundle);
formatBundleMsg(b, 'plotly.js');
function bundle() {
b.bundle(function(err) {
if(err) console.error(JSON.stringify(String(err)));
if(firstBundle) {
onFirstBundleCallback();
firstBundle = false;
}
})
.pipe(
fs.createWriteStream(constants.pathToPlotlyBuild)
);
}
return bundle;
};
function formatBundleMsg(b, bundleName) {
var msgParts = [
bundleName, ':', '',
'written', 'in', '', 'sec',
'[', '', ']'
];
b.on('bytes', function(bytes) {
msgParts[2] = prettySize(bytes, true);
});
b.on('time', function(time) {
msgParts[5] = (time / 1000).toFixed(2);
});
b.on('log', function() {
var formattedTime = common.formatTime(new Date());
msgParts[msgParts.length - 2] = formattedTime;
console.log(msgParts.join(' '));
});
}
|